简体   繁体   中英

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

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.

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

You can use array reduce to calculate the sum:

 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++){ let eachScore = employeeList[i].scores; let b = eachScore.reduce((total,current)=>{ total += +current; return total; },0); console.log(b); sum += b; } console.log(sum); 

Maybe this will help:

var parsedScores = [];
var scores = [
   "4","2","5","1","3","2","2","1","3","2"
];
let total = 0;
scores.forEach(el => {
   parsedScores.push(parseInt(el))
})
for (let score in parsedScores) {
   total += parsedScores[score]
}
console.log(total);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM