1

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]]
  };
});
1

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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);

4 Comments

Or with implicit return: data.reduce((a,c)=>((a[c.name]??=[]).push(c.value), a),{});
I know this is a "code only" answer. But its actions are so simple they speak for themselves.
@RokoC.Buljan with destructuring, console.log(data.reduce((a, {name, value}) => ((a[name]??=[]).push(value), a), {}));
In the end it boils down to a question of style: Do I strive for least repetition, shortest code or best readability?
1

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]
}

Comments

0

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:

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.