I have the following array for example -
[
{
name: "abc",
value: "1"
},
{
name: "xyz",
value: "2"
},
{
name: "abc",
value: "3"
},
{
name: "abc",
value: "4"
},
{
name: "xyz",
value: "5"
},
]
Now, I want to reduce this array into a single object by grouping value
with the same name
together into an array. Something like this -
{
abc: [1, 3, 4],
xyz: [2, 5]
}
How do I do this using reduce
in JavaScript?
I'm not sure if this the right approach but I have tried something like this and it doesn't give me the desired output.
const data = arr.reduce((acc, item) => {
return {
...acc,
[item.name]: [item.value, acc[item.value]]
};
});
CodePudding user response:
const data=[
{
name: "abc",
value: "1"
},
{
name: "xyz",
value: "2"
},
{
name: "abc",
value: "3"
},
{
name: "abc",
value: "4"
},
{
name: "xyz",
value: "5"
},
];
const res=data.reduce((a,c)=>{
(a[c.name]??=[]).push(c.value);
return a;
},{});
console.log(res);
CodePudding user response:
You were on the right track with your answer, but you need to check if acc
already has a key of item.name
:
const data = [{
name: "abc",
value: "1"
},
{
name: "xyz",
value: "2"
},
{
name: "abc",
value: "3"
},
{
name: "abc",
value: "4"
},
{
name: "xyz",
value: "5"
},
]
const result = data.reduce((acc, { name, value }) => ({
...acc,
[name] : [...(acc[name] || []), value]
}), {})
console.log(result)
Note you can use object destructuring to simplify the accumulation code.
CodePudding user response:
const data = [{
name: "abc",
value: "1"
},
{
name: "xyz",
value: "2"
},
{
name: "abc",
value: "3"
},
{
name: "abc",
value: "4"
},
{
name: "xyz",
value: "5"
},
];
let finalObject = {};
data.forEach((item) => {
if (!finalObject.hasOwnProperty(item.name)) {
finalObject[item.name] = [item.value];
} else {
finalObject[item.name].push(item.value);
}
});
console.log(finalObject);
Just parse values if you need in string to number and it will give you answer
{
abc: [1, 3, 4],
xyz: [2, 5]
}
CodePudding user response:
You can group the array using Array.prototype.reduce.
const
data = [
{ name: "abc", value: "1" },
{ name: "xyz", value: "2" },
{ name: "abc", value: "3" },
{ name: "abc", value: "4" },
{ name: "xyz", value: "5" },
],
result = data.reduce((r, o) => ((r[o.name] ??= []).push(o.value), r), {});
console.log(result);
reduce
takes some time to getting used to, but it's fairly simple once it clicks. You can also group the array using a simple for...of
loop.
const data = [
{ name: "abc", value: "1" },
{ name: "xyz", value: "2" },
{ name: "abc", value: "3" },
{ name: "abc", value: "4" },
{ name: "xyz", value: "5" },
];
const result = {};
for (let d of data) {
(result[d.name] ??= []).push(d.value);
}
console.log(result);
Other relevant documentations:
CodePudding user response:
Use reduce()
:
const arr = [
{
name: "abc",
value: "1"
},
{
name: "xyz",
value: "2"
},
{
name: "abc",
value: "3"
},
{
name: "abc",
value: "4"
},
{
name: "xyz",
value: "5"
},
]
const data = arr.reduce((a,c) => ({...a, [c.name]: a[c.name] ? [...a[c.name], c.value] : [c.value]}),{})
console.log(data)