Can you provide an efficient way to combine array values from JavaScript objects that have a common key?
How would you rearrange the contents of the array
into the format of output
? In this scenario, all value
keys (whether single value or array) should be merged within objects that share the same name
key.
var array = [
{
name: "foo1",
value: "val1"
}, {
name: "foo1",
value: [
"val2",
"val3"
]
}, {
name: "foo2",
value: "val4"
}
];
var output = [
{
name: "foo1",
value: [
"val1",
"val2",
"val3"
]
}, {
name: "foo2",
value: [
"val4"
]
}
];