0

How do I convert an object like:

{
  "28fca3e7-7b5a-95b1-205c-03ff199dc2b4": 6,
  "426fb283-6fa9-a58c-e896-f795a5d42c50": 8,
  "629fe212-0d74-f5ce-d476-baa527252ffb": 12,
} 

Into an array like:

[{
  "id": "28fca3e7-7b5a-95b1-205c03ff199dc2b4",
  "count": 6
},
{
  "id": "629fe212-0d74-f5ce-d476-baa527252ffb",
  "count": 8
},
{
  "id": "426fb283-6fa9-a58c-e896-f795a5d42c50",
  "count": 12,
}]

I want to specifically add the keys "id" and "count" to the elements in each object of the array.

I have already converted the object to an array using this code:-

Object.keys(arr).map(key => ({[key]: arr[key]}));

And the result is this:-

[
  {
    "28fca3e7-7b5a-95b1-205c-03ff199dc2b4": 6,
  },

  {
    "629fe212-0d74-f5ce-d476-baa527252ffb": 8,
  },

  {
    "426fb283-6fa9-a58c-e896-f795a5d42c50": 12,
  },
]

I would like to add the above-mentioned keys("id" and "count") to this array.

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
  • 3
    Does this answer your question? [How to convert an Object {} to an Array \[\] of key-value pairs in JavaScript](https://stackoverflow.com/questions/38824349/how-to-convert-an-object-to-an-array-of-key-value-pairs-in-javascript) – Edison Pebojot Apr 09 '21 at 01:02

6 Answers6

2

You may use "getOwnPropertyName" to get all keys of object and assign them to a list or array. Let me know if my answer is helpful!

var text = {
    "28fca3e7-7b5a-95b1-205c-03ff199dc2b4": 6,
    "426fb283-6fa9-a58c-e896-f795a5d42c50": 8,
    "629fe212-0d74-f5ce-d476-baa527252ffb": 12};

var keys = Object.getOwnPropertyNames(this.text);

keys.forEach(x => {
      let newKeyObject = new keyObject();
      newKeyObject.key = x;
      newKeyObject.count = this.text[x];
      this.keyObjectList.push(newKeyObject);
    });

console.log(JSON.stringify(this.keyObjectList));
Yisi Tan
  • 309
  • 2
  • 5
0

Like this:

const obj = {
  "28fca3e7-7b5a-95b1-205c-03ff199dc2b4": 6,
  "426fb283-6fa9-a58c-e896-f795a5d42c50": 8,
  "629fe212-0d74-f5ce-d476-baa527252ffb": 12,
};

const arr = Object.keys(obj).map(k => ({
  id: k,
  count: obj[k],
}));
see sharper
  • 11,505
  • 8
  • 46
  • 65
0

just add the keys you want and put those values on there. try this.

Object.keys(arr).map(key => ({"id":key, "count": arr[key]}))
0

You'll use the Object.keys() iterator to get an array of keys, and then transform that array into an array of objects.

const obj = {
   a: 1,
   b: 2,
   c: 3,
};

console.log(Object.keys(obj).map(key => ({id: key, count: obj[key]})));
mjk
  • 2,443
  • 4
  • 33
  • 33
Ragul CS
  • 417
  • 3
  • 11
0

Simpler than mapping from the keys is map Object.entries()

const data = {
  "28fca3e7-7b5a-95b1-205c-03ff199dc2b4": 6,
  "426fb283-6fa9-a58c-e896-f795a5d42c50": 8,
  "629fe212-0d74-f5ce-d476-baa527252ffb": 12,
}, 

res = Object.entries(data).map(([id, count]) => ({id, count}))

console.log(res)
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

You can simplify use Object.entries combined with Array#map like this

const object = {"28fca3e7-7b5a-95b1-205c-03ff199dc2b4":6,"426fb283-6fa9-a58c-e896-f795a5d42c50":8,"629fe212-0d74-f5ce-d476-baa527252ffb":12,};

const result = Object.entries(object).map(([key, value]) => ({Id: key, count: value}));
console.log(result);

The Object.entries() method returns an array of a given object's own enumerable string-keyed property [key, value] pairs

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56