我想将对象数组聚合成单个对象,每个对象都有不同的值。但是我的代码没有给我想要的输出。有人可以帮帮我吗?
My Input :
[{
"obj1" : "value1",
"obj2" : ["abc", "def"],
"obj3" : ["ghi"],
"obj4" : "value4",
},
{
"obj1" : "value2",
"obj2" : ["abc", "mno"],
"obj3" : ["klm"],
"obj4" : "value4",
}]
Output I want:
{
"obj1" : ["value1","value2"]
"obj2" : ["abc", "def","mno"],
"obj3" : ["ghi","klm"],
"obj4" : ["value4"]
}
My Code:
const result = filterData.reduce((a,c) =>
(Object.keys(c).map(k => a[k] = [...a[k] || [], c[k]]), a), {})