问题描述
[
{
value1: {
something: "something"
}
},{
value2: {
something: "something"
}
}
]
我需要收到这样的信息:
{
value1: {
something: "something"
},value2: {
something: "something"
}
}
提前致谢!
解决方法
只需对原始数组进行缩减
const data = [
{
value1: {
something: "something"
}
},{
value2: {
something: "something"
}
}
]
const result = data.reduce((acc,cur) => {
const keys = Object.keys(cur);
keys.forEach(key => {
if(!acc[key]) {
acc[key] = {...cur[key]}
}
});
return acc;
},{});
console.log(result);
/*
{
value1: { something: 'something' },value2: { something: 'something' }
}
*/
,
const something = 3
console.log([{value1: {something}},{value2: {something}}].reduce((acc,x) => ({...acc,...x})))