I'm trying to merge an array of objects into a single object, ensuring that each value is distinct. However, my current code isn't producing the output I want. Can someone assist me with this?
Sample Input:
[{
"obj1" : "value1",
"obj2" : ["abc", "def"],
"obj3" : ["ghi"],
"obj4" : "value4",
},
{
"obj1" : "value2",
"obj2" : ["abc", "mno"],
"obj3" : ["klm"],
"obj4" : "value4",
}]
Desired Output:
{
"obj1" : ["value1","value2"]
"obj2" : ["abc", "def","mno"],
"obj3" : ["ghi","klm"],
"obj4" : ["value4"]
}
My Current Code:
const result = filterData.reduce((a,c) => (Object.keys(c).map(k => a[k] = [...a[k] || [], c[k]]), a), {})