How to group by more than one key and sum its values in an array of objects, using Lodash?

Mirco Cattabriga

In the array below, I want to group by item and reason, then calculate the total quantity:

item_movements = [
  {
    item: "Apple",
    reason: 1,
    quantity: 5
  },
  {
    item: "Banana",
    reason: 2,
    quantity: 10
  },
  {
    item: "Apple",
    reason: 1,
    quantity: 5
  },
  {
    item: "Banana",
    reason: 1,
    quantity: 8
  },
  {
    item: "Apple",
    reason: 2,
    quantity: 3
  },
  {
    item: "Banana",
    reason: 1,
    quantity: 8
  }
];

The result I am trying to achieve is this:

item_totals = [
  {
    item: "Apple",
    1: 10,
    2: 3
  },
  {
    item: "Banana",
    1: 16,
    2: 10
  }
];

I was able to group by item using Loadash like so:

({
  itemMovementbyItem() {
    var result = _(this.item_movements)
      .groupBy(x => x.item)
      .map((value, key) => ({
        item: key,
        item_movements: value
      }))
      .value()

    return result
  }
})

But I am having problems to group a second time by reason.

Nina Scholz

You could sum the properies directly after grouping by 'item'.

function itemMovementbyItem(array) {
    return _(array)
        .groupBy('item')
        .map(group => ({
            item: group[0].item,
            1: _.sumBy(group, o => o.reason === 1 ? o.quantity : 0),
            2: _.sumBy(group, o => o.reason === 2 ? o.quantity : 0)
        }))
        .value();
}

var item_movements = [{ item: "Apple", reason: 1, quantity: 5 }, { item: "Banana", reason: 2, quantity: 10 }, { item: "Apple", reason: 1, quantity: 5 }, { item: "Banana", reason: 1, quantity: 8 }, { item: "Apple", reason: 2, quantity: 3 }, { item: "Banana", reason: 1, quantity: 8 }];

console.log(itemMovementbyItem(item_movements));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using underscore groupby to group an array of objects by more than one attribute

How to reduce an array of objects by key and sum its values in javascript

group array of objects by more than one parameter

How to get values from an array of objects using a KEY and creating a new array of values with LODASH

Using Lodash to sum values by key

How to group and sum values from array of objects?

To sum up more than one array values in hash - Ruby

How can i iterate over array of objects and change group label when its different than last one?

How to fill the array of sets by key and sum the values Lodash.js

Group Array of Objects by key and sum values from mongoose aggregation result

lodash group array of nested objects by key

HIVE - Group by more than one column and Sum

How to group an array of objects by key using javascript?

How to add more than one values to a key in qjsonobject

lodash/underscore find objects by key that is in values of array

How to group Ruby enumerable/array by more than one field?

Group and sum array of obects using Lodash

How to count multiple properties values in array of objects using lodash?

How to merge more than one list having objects into one list with sum of one of the elements of the list in c#

How to get sum of all key values in array of objects without using any built in function

Group Array of objects into specific format using lodash

How to remove values from an array if occurring more than one time?

lodash sum of array of objects

Group array by more than one property swift

Group and sum of an array of objects based on number key

Check if a JavaScript array contains more than one element... with lodash?

Typescript/Lodash group by -- Trying to group values of a key of an array with their quantity

How to return the key of an array of objects using its name

Replace array value with more than one values