我有一个像这样的对象数组:
currentArray = [
{
year: 2011,
asset: {
silver: 322,
gold: 325,
},
},
{
year: 2012,
asset: {
silver: 411,
gold: 2235,
},
},
];
在JavaScript/TypeScript中需要做些什么,把它从当前的结构改成下面的结构?每个资产对象的金银属性保存在上面的对象中,"year"后面,需要删除资产对象:
desiredArray = [
{
year: 2011,
silver: 322,
gold: 325,
},
{
year: 2012,
silver: 411,
gold: 2235,
},
];
let currentArray = [{
year: 2011,
asset: {
silver: 322,
gold: 325,
},
},
{
year: 2012,
asset: {
silver: 411,
gold: 2235,
},
},
];
currentArray.map(item => {
Object.assign(item, item.asset)
delete item.asset;
return item
})
console.log(currentArray)