1

Let's say I have an array of objects:

[
    {
        category: 'Category A',
        max: 10,
        min: 12
    },

    {
        category: 'Category B',
        max: 2,
        min: 1
    },

    {
        category: 'Category C',
        max: 4,
        min: 4
    }
]

However, I can't seem to get an array that looks like this:

{
    category: 'Category ALL',
    max: 16,
    min: 17
}

All my attempts to make through reduce did not work.

Paul
  • 139,544
  • 27
  • 275
  • 264
LuisDemn
  • 19
  • 4

4 Answers4

2

You could reduce the data by taking min and max as sum in an object for the next iteration.

var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
    result = Object.assign(
        { category: 'Category All' }, 
        data.reduce((a, b) => ({ min: a.min + b.min, max: a.max + b.max }))
    );
    
console.log(result);

A more dynamic approach by using an array for the keys.

var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
    keys = ['min', 'max'],
    result = Object.assign(
        { category: 'Category All' }, 
        data.reduce((a, b) => Object.assign(...keys.map(k => ({ [k]: a[k] + b[k] }))))
    );
    
console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

var array = [{
    category: 'Category A',
    max: 10,
    min: 12
  },

  {
    category: 'Category B',
    max: 2,
    min: 1
  },

  {
    category: 'Category C',
    max: 4,
    min: 4
  }
]

var result = array.reduce((a, v) => {
  a.max += v.max
  a.min += v.min
  return a
}, {
  category: "Category All",
  max: 0,
  min: 0
})

console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
George
  • 6,630
  • 2
  • 29
  • 36
0

Just pass in the initial object { category: 'Category ALL', max: 0, min: 0 } to reduce, and for each item, just add its max and min to that object:

let result = array.reduce((acc, obj) => {
    acc.max += obj.max;
    acc.min += obj.min;
    return acc;
}, { category: 'Category ALL', max: 0, min: 0 });

Example:

let array = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }];

let result = array.reduce((acc, obj) => {
    acc.max += obj.max;
    acc.min += obj.min;
    return acc;
}, { category: 'Category ALL', max: 0, min: 0 });

console.log(result);
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
0

You could use Array.reduce, so the array of objects can be reduced into a single object, and using es6 you can write in a single line.

Please see the below code,i hope it solves the issue

var res = [
    {
        category: 'Category A',
        max: 10,
        min: 12
    },

    {
        category: 'Category B',
        max: 2,
        min: 1
    },

    {
        category: 'Category C',
        max: 4,
        min: 4
    }
]

var result = res.reduce((a,c) =>  ({category:"Category All", max: a.max + c.max, min: a.min + c.min}))

console.log("result", result)
Learner
  • 8,379
  • 7
  • 44
  • 82