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.

1

6 Answers 6

3

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));
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

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],
}));

Comments

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]}))

Comments

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

1 Comment

Does this answer your question? Let me know if you need any help ^^! @Suprateek Korukonda stackoverflow.com/help/someone-answers
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]})));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.