2021年4月8日星期四

How to convert an object into array and add keys

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.

https://stackoverflow.com/questions/67013685/how-to-convert-an-object-into-array-and-add-keys April 09, 2021 at 08:56AM

没有评论:

发表评论