For a react project I need to simplify an array of objects.
I've an array of objects coming from wp rest api axios request. Inside objects there are objects. I would like to "remove" this nested object in order to have this :
[
{
"id": 101,
"title": "CTC20180018",
"fielda": "valuea",
"fieldb": "valueb",
"fieldc": "valuec"
},
{
"id": 102,
"title": "D2021063365",
"fielda": "valuea",
"fieldb": "valueb",
"fieldc": "valuec"
},
...
]
What is the best solution ? .map() the array and use destructuring ?
The original array :
[
{
"id": 101,
"title": {
"rendered": "CTC20180018"
},
"acf": {
"fielda": "valuea",
"fieldb": "valueb",
"fieldc": "valuec"
}
},
{
"id": 102,
"title": {
"rendered": "D2021063365"
},
"acf": {
"fielda": "valuea",
"fieldb": "valueb",
"fieldc": "valuec"
}
},
...
]
acfandrenderedneed to be removed - in order to achieve the target structure, is that correct?const newArr = origArr.map(obj => ({ id: obj.id, title: obj.title.rendered, ...obj.acf}));Or, you may use kiranvj's solution below. It uses destructuring the iterator.