Javascript ES6 calculate the sum of an array of arrays of objects

pengz

I have multiple arrays of objects with a string property "CountType" and a number property "ItemCount".

The objects must be added up to calculate the total of all items by "CountType" property.

For example, I need to know the total number of Volumes, Sheets, Photos, etc.

Attempt #1: This code I attempted returns 0. Possibly that is because I need to loop through the outer array to do the comparison across the nested arrays.

function sumByProperty(items, prop) {
  if (items == null) {
      return 0;
  }
  return items.reduce(function (a, b) {
      return b[prop] == null ? a : a + b[prop];
  }, 0);
}

INPUT

Item count

[
    [
      {"CountType":"Volumes","ItemCount":3},
      {"CountType":"Sheets","ItemCount":6},
      {"CountType":"Photos","ItemCount":3},
      {"CountType":"Boxes","ItemCount":1},
      {"CountType":"Other","ItemCount":2}
    ],
    [
      {"CountType":"Volumes","ItemCount":1},
      {"CountType":"Sheets","ItemCount":1},
      {"CountType":"Photos","ItemCount":3},
      {"CountType":"Boxes","ItemCount":0},
      {"CountType":"Other","ItemCount":1}
    ],
    [
      {"CountType":"Volumes","ItemCount":1},
      {"CountType":"Sheets","ItemCount":0},
      {"CountType":"Photos","ItemCount":3},
      {"CountType":"Boxes","ItemCount":4},
      {"CountType":"Other","ItemCount":5}
    ]
]

DEISRED OUTPUT

Total count

[
  {"CountType":"Volumes","ItemCount":5},
  {"CountType":"Sheets","ItemCount":7},
  {"CountType":"Photos","ItemCount":9},
  {"CountType":"Boxes","ItemCount":5},
  {"CountType":"Other","ItemCount":8}
]

UPDATE: Here is how I am trying to run the function:

https://jsfiddle.net/yd051o76/2/

mwilson

Here's an example using 2 reduce functions with a for ... in ... to merge the results back into the parent reducer.

The trick is to make sure you initialize your accumulator as null so you can define your own. Then, it's just a matter of reducing the array of arrays and having another reducer to run the array of objects. Once done, you just need to merge the result of the inner array back into the parent. (Might be a better way to do this part)

Fiddle: https://jsfiddle.net/mswilson4040/jcp9x6ba/26/

const json = [
  [
    {"CountType":"Volumes","ItemCount":3},
    {"CountType":"Sheets","ItemCount":6},
    {"CountType":"Photos","ItemCount":3},
    {"CountType":"Boxes","ItemCount":1},
    {"CountType":"Other","ItemCount":2}
  ],
  [
    {"CountType":"Volumes","ItemCount":1},
    {"CountType":"Sheets","ItemCount":1},
    {"CountType":"Photos","ItemCount":3},
    {"CountType":"Boxes","ItemCount":0},
    {"CountType":"Other","ItemCount":1}
  ],
  [
    {"CountType":"Volumes","ItemCount":1},
    {"CountType":"Sheets","ItemCount":0},
    {"CountType":"Photos","ItemCount":3},
    {"CountType":"Boxes","ItemCount":4},
    {"CountType":"Other","ItemCount":5}
  ]
];

const counts = json.reduce( (acc, currentArray) => {
	acc = acc ? acc : {};
  const arrCounts = currentArray.reduce( (_acc, _item) => {
  	_acc = _acc ? _acc : {};
    _acc[_item.CountType] = _acc[_item.CountType] ? _acc[_item.CountType] + _item.ItemCount : _item.ItemCount;
    return _acc;
  }, null);
  for (const item in arrCounts) {
  	acc[item] = acc[item] ? acc[item] + arrCounts[item] : arrCounts[item];
  }
  return acc;
}, null);


console.log(counts);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

es6 filter an array of objects by an array of filter objects

分類Dev

How to get a the sum of multiple arrays within an array of objects?

分類Dev

Javascript Array ES6 Undefined Error

分類Dev

Sum arrays in javascript

分類Dev

Convert array of objects to array of arrays

分類Dev

javascript sum multidimensional array

分類Dev

Remove empty objects from an object using recursion (using vanila ES6 javascript)

分類Dev

Iterate Nested Value in Array in JavaScript/ES6/ES7

分類Dev

javascript array of objects - redistribute

分類Dev

Javascript, Array of Objects?

分類Dev

Javascript - sum 2d arrays

分類Dev

How to sum two arrays data in JavaScript

分類Dev

Query Mongodb Sum of firsts element of an array of objects

分類Dev

Javascript: Split array of arrays into multiple arrays

分類Dev

Javascript sum inner arrays in 3d nested arrays

分類Dev

JavaScript - "Combining" two similar arrays of objects

分類Dev

Algorithm mixing information of two arrays of objects in javascript?

分類Dev

Filter arrays in Javascript and return all related objects

分類Dev

Group query in mongoDB using array items to calculate sum

分類Dev

How to iterate multi dimensional array and calculate sum for each row in php?

分類Dev

Cartesian product of array of objects in javascript

分類Dev

Not able to transform array of objects in JavaScript

分類Dev

filter two array of objects in javascript

分類Dev

How to create an array of objects in JavaScript?

分類Dev

Sort by multiple objects in a array in Javascript

分類Dev

Destruct nested array of objects javascript

分類Dev

How to delegate to an array of objects in JavaScript

分類Dev

Sum and average values of multiple objects in an array into one array

分類Dev

How to get the combination of array values from nested arrays in an array of objects

Related 関連記事

  1. 1

    es6 filter an array of objects by an array of filter objects

  2. 2

    How to get a the sum of multiple arrays within an array of objects?

  3. 3

    Javascript Array ES6 Undefined Error

  4. 4

    Sum arrays in javascript

  5. 5

    Convert array of objects to array of arrays

  6. 6

    javascript sum multidimensional array

  7. 7

    Remove empty objects from an object using recursion (using vanila ES6 javascript)

  8. 8

    Iterate Nested Value in Array in JavaScript/ES6/ES7

  9. 9

    javascript array of objects - redistribute

  10. 10

    Javascript, Array of Objects?

  11. 11

    Javascript - sum 2d arrays

  12. 12

    How to sum two arrays data in JavaScript

  13. 13

    Query Mongodb Sum of firsts element of an array of objects

  14. 14

    Javascript: Split array of arrays into multiple arrays

  15. 15

    Javascript sum inner arrays in 3d nested arrays

  16. 16

    JavaScript - "Combining" two similar arrays of objects

  17. 17

    Algorithm mixing information of two arrays of objects in javascript?

  18. 18

    Filter arrays in Javascript and return all related objects

  19. 19

    Group query in mongoDB using array items to calculate sum

  20. 20

    How to iterate multi dimensional array and calculate sum for each row in php?

  21. 21

    Cartesian product of array of objects in javascript

  22. 22

    Not able to transform array of objects in JavaScript

  23. 23

    filter two array of objects in javascript

  24. 24

    How to create an array of objects in JavaScript?

  25. 25

    Sort by multiple objects in a array in Javascript

  26. 26

    Destruct nested array of objects javascript

  27. 27

    How to delegate to an array of objects in JavaScript

  28. 28

    Sum and average values of multiple objects in an array into one array

  29. 29

    How to get the combination of array values from nested arrays in an array of objects

ホットタグ

アーカイブ