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

walkforr

I was wondering how you get the sum of multiple arrays within an array of objects. My code is as follows:

const employeeList = [    

    {
    "name": "Ahmed",
    "scores": [
    "5",
    "1",
    "4",
    "4",
    "5",
    "1",
    "2",
    "5",
    "4",
    "1"
    ]
    },
    {
    "name": "Jacob Deming",
    "scores": [
    "4",
    "2",
    "5",
    "1",
    "3",
    "2",
    "2",
    "1",
    "3",
    "2"
    ]
    }];

var sum = 0;

for(let i = 0; i < employeeList.length; i++){
  var eachScore = employeeList[i].scores;
  const b = eachScore.map(Number);
  console.log(b);

  sum += parseInt(b);//this is the code that doesn't work

}

console.log(sum);

So the problem is, I can get the two arrays to console log but I'm not sure how to go about summing up each array.. When I do sum += parseInt(b), it just logs how many items are in the array(9). and When I do without parseInt, it concats the numbers together but doesn't sum them up.. I would like to use a .split() method to split the arrays and sum them up individually but I haven't quite figured out how to do it yet.

CertainPerformance

Because b is an array of numbers, you can't meaningfully use + with it unless you want a comma-joined string. The most functional way to sum up an array is to use reduce, which can be used to iterate over its items and add them all to the accumulator:

b.reduce((a, b) => a + b);

If you want to know the partial sums, I'd use .map to transform each object in the employeeList array into the sum of their scores, by extracting the scores property and using reduce to sum them all up:

const employeeList=[{"name":"Ahmed","scores":["5","1","4","4","5","1","2","5","4","1"]},{"name":"Jacob Deming","scores":["4","2","5","1","3","2","2","1","3","2"]}]

const sum = (a, b) => Number(a) + Number(b);
const output = employeeList.map(({ scores }) => scores.reduce(sum));
console.log(output);
// If you want to sum up the results into a single number as well:
console.log(output.reduce(sum));

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

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

分類Dev

How to get the sum of number from an array of objects, using pure JS?

分類Dev

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

分類Dev

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

分類Dev

How get multiple SUM() for different columns with GROUP BY

分類Dev

How to unpack multiple dictionary objects inside list within a row of dataframe?

分類Dev

How to sum values column wise for Array of Arrays in Scala?

分類Dev

How to get a sum of an array in Nodejs using foreach?

分類Dev

PHP - How to get the sum of a multidimensional array?

分類Dev

How to get sum of array in foreach in laravel 6

分類Dev

How to get unique an array of objects back from a complex an array of objects?

分類Dev

How to make an array of multiple nested objects?

分類Dev

How do you map an array of objects within an object?

分類Dev

Convert array of objects to array of arrays

分類Dev

How to parse multiple nested JSON objects/arrays with GSON?

分類Dev

How to find sum objects inside an array using mongoose ,node ,

分類Dev

How to find sum objects inside an array using mongoose ,node ,

分類Dev

Max value within array of objects

分類Dev

Grouping and summing within an array of objects

分類Dev

How to get an Array of Objects from Firestore in Swift?

分類Dev

How to get all values of objects inside array

分類Dev

How to search for a value in array of objects and get it in Laravel?

分類Dev

How can I convert an array of arrays to an array of objects, selecting the key from other array?

分類Dev

How to get an array of counts of specific objects in an existing array?

分類Dev

How to get a particular attribute from an array of array objects?

分類Dev

How do I get the sum of multiple columns dynamically?

分類Dev

How to parse json to get all values of a specific key within an array?

分類Dev

Sum and average of numbers within an array C

分類Dev

Get SUM of query result WITHIN same query

Related 関連記事

  1. 1

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

  2. 2

    How to get the sum of number from an array of objects, using pure JS?

  3. 3

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

  4. 4

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

  5. 5

    How get multiple SUM() for different columns with GROUP BY

  6. 6

    How to unpack multiple dictionary objects inside list within a row of dataframe?

  7. 7

    How to sum values column wise for Array of Arrays in Scala?

  8. 8

    How to get a sum of an array in Nodejs using foreach?

  9. 9

    PHP - How to get the sum of a multidimensional array?

  10. 10

    How to get sum of array in foreach in laravel 6

  11. 11

    How to get unique an array of objects back from a complex an array of objects?

  12. 12

    How to make an array of multiple nested objects?

  13. 13

    How do you map an array of objects within an object?

  14. 14

    Convert array of objects to array of arrays

  15. 15

    How to parse multiple nested JSON objects/arrays with GSON?

  16. 16

    How to find sum objects inside an array using mongoose ,node ,

  17. 17

    How to find sum objects inside an array using mongoose ,node ,

  18. 18

    Max value within array of objects

  19. 19

    Grouping and summing within an array of objects

  20. 20

    How to get an Array of Objects from Firestore in Swift?

  21. 21

    How to get all values of objects inside array

  22. 22

    How to search for a value in array of objects and get it in Laravel?

  23. 23

    How can I convert an array of arrays to an array of objects, selecting the key from other array?

  24. 24

    How to get an array of counts of specific objects in an existing array?

  25. 25

    How to get a particular attribute from an array of array objects?

  26. 26

    How do I get the sum of multiple columns dynamically?

  27. 27

    How to parse json to get all values of a specific key within an array?

  28. 28

    Sum and average of numbers within an array C

  29. 29

    Get SUM of query result WITHIN same query

ホットタグ

アーカイブ