0

I have a question for this case

Input:

data = [
{ number_1: 10, number_2: 20 },
{ number_1: 15, number_2: 5 },
{ number_1: 15, number_2: 35 },
];

Output:

dataSum = { sum_number_1: 40, sum_number_2: 60 };

I want to ask for 2 method we can use: array.reduce and array.forEach. And I see we need to transform array to an object for output. Thanks for your help.

4
  • Welcome to Stack Overflow! Please take the tour (you get a badge!) and read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help. Commented Nov 3, 2020 at 7:42
  • "I want to ask for 2 method we can use: array.reduce and array.forEach." Don't use reduce for this, it overcomplicates things. (In general, if you're not doing functional programming with predefined, reusable reducer functions, don't use reduce.) You'll see reduce used a lot, almost always where the code would be simpler and clearer with a loop. I also wouldn't use forEach, I'd use a for-of loop, but forEach can work as well. Commented Nov 3, 2020 at 7:43
  • Basically: Create a result object with those two properties and the values 0. Loop through the array adding the property values from each object to the values in your result object. Commented Nov 3, 2020 at 7:44
  • Looks like an assignment/exercise to me :( Just try to do it and show what you have done. Otherwise, SE is the wrong place. Commented Nov 4, 2020 at 9:12

4 Answers 4

2

The initial schema for Array.reduce is really up to you.

const data = [{
    number_1: 10,
    number_2: 20
  },
  {
    number_1: 15,
    number_2: 5
  },
  {
    number_1: 15,
    number_2: 35
  },
];

const res = data.reduce((acc, {
  number_1,
  number_2
}) => {
  acc.sum_number_1 += number_1;
  acc.sum_number_2 += number_2;

  return acc;
}, {
  sum_number_1: 0,
  sum_number_2: 0
})

console.log(res)

And here's forEach

const data = [{
    number_1: 10,
    number_2: 20
  },
  {
    number_1: 15,
    number_2: 5
  },
  {
    number_1: 15,
    number_2: 35
  },
];

const res = {
  sum_number_1: 0,
  sum_number_2: 0
};

data.forEach(({
  number_1,
  number_2
}) => {
  res.sum_number_1 += number_1;
  res.sum_number_2 += number_2;
})

console.log(res)

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

Comments

2

Is this what you want?

data.reduce((acc, {number_1, number_2}) => {
    acc.sum_number_1 += number_1;
    acc.sum_number_2 += number_2;
    return acc;
},{sum_number_1: 0, sum_number_2: 0})

Comments

2

Try like this:

data = [
   { number_1: 10, number_2: 20 },
   { number_1: 15, number_2: 5 },
   { number_1: 15, number_2: 35 },
];

const reducer = (accumulator, currentValue) => {
  accumulator.sum_number_1 += currentValue.number_1;
  accumulator.sum_number_2 += currentValue.number_2;
  return accumulator;
};

console.log(data.reduce(reducer, { sum_number_1: 0, sum_number_2: 0 }));

The reduce function is used with an initial value structured as the final object to be returned. The numbers are summed in the callback function to then return the result.

Comments

2

You could take a dynamic approach which sums all properties of an object.

const
    data = [{ number_1: 10, number_2: 20 }, { number_1: 15, number_2: 5 }, { number_1: 15, number_2: 35 }],
    result = data.reduce((r, o) => {
        Object.entries(o).forEach(([k, v]) => r[k] = (r[k] || 0) + v)
        return r;
    });

console.log(result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.