I have an array of objects each object has one nested object I need to modify that
Example: What I have below
const array = [{
asset: {key: '1235', type: 'mocFirst'},
id: 27,
marketValuey: 6509,
marketValueySecond: 65033,
marketValueyThird: 650900,
}]
I want get that:
const array = [{
type: 'mocFirst'
key: '1235',
id: 27,
marketValuey: 6509,
marketValueySecond: 65033,
marketValueyThird: 650900,
}]
There is my solution
const array = [{
asset: {key: '1235', type: 'mocFirst'},
id: 27,
marketValuey: 6509,
marketValueySecond: 65033,
marketValueyThird: 650900,
},
{
asset: {key: '12', type: 'mocFirst44'},
id: 27,
marketValuey: 6409,
marketValueySecond: 64033,
marketValueyThird: 640900,
},
{
asset: {key: '1299', type: 'mocFirst'},
id: 271,
marketValuey: 6109,
marketValueySecond: 61033,
marketValueyThird: 610900,
},
{
asset: {key: '1296', type: 'mocFirst'},
id: 272,
marketValuey: 65092,
marketValueySecond: 650332,
marketValueyThird: 6509020,
},
]
const resultArr = array.map(item => {
const { asset, ...newObj} = item;
const { key, type } = item.asset;
return { key, type, ...newObj};
});
Any things about my solution? Maybe it can be done better? In production, I will have a big array