pops = [
[
{
key: 'test1',
value: '0'
}
],
[
{
key: 'test2',
value: '0'
},
{
key: 'test3',
value: '0'
}
]
];需要转换成这种格式
formated =
[{
'test1':'0',
},
{
'test2':'0',
'test3':'0'
}]我试着用这个代码来解决问题,但没有成功。有人能帮我解决这个问题吗?谢谢。
let formated = []
let count=0
for(let item of this.pops){
for(let prop of item){
formated[count].push(prop.key:prop.value)
}
count++;
}发布于 2021-08-23 13:01:43
这可以通过map和reduce的组合来实现。
const pops = [
[
{
key: 'test1',
value: '0'
}
],
[
{
key: 'test2',
value: '0'
},
{
key: 'test3',
value: '0'
}
]
];
const formatted = pops.map(i => i.reduce( (acc,x) => ({...acc,[x.key]:x.value}),{}));
console.log(formatted);
https://stackoverflow.com/questions/68893162
复制相似问题