LuisDemn
LuisDemn

Reputation: 19

Reduce Multiple Objects into One

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.

Upvotes: 1

Views: 1699

Answers (4)

Beginner
Beginner

Reputation: 9095

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)

Upvotes: 0

ibrahim mahrir
ibrahim mahrir

Reputation: 31682

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

Upvotes: 0

George
George

Reputation: 6739

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

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386550

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

Upvotes: 2

Related Questions