I have this code
const jsonArray = [1,2,3,4,5];
I want to convert it so I can get
{
"id" : 1
},
{
"id" : 2
},
{
"id" : 3
},
{
"id" : 4
},
{
"id" : 5
}
So I need to add "id" key for all the objects outputs
I have this code
const jsonArray = [1,2,3,4,5];
I want to convert it so I can get
{
"id" : 1
},
{
"id" : 2
},
{
"id" : 3
},
{
"id" : 4
},
{
"id" : 5
}
So I need to add "id" key for all the objects outputs
Try the map function
jsonObjectArray = jsonArray.map((ele) => {return {"id": ele}})
You could use Array.prototype.map() for that:
const jsonArray = [1,2,3,4,5];
const arrayofObjects = jsonArray.map(e => ({id: e}))
console.log(arrayofObjects);