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

给你一囗甜甜゛ 提交于 2021-02-10 14:46:33

问题


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.


回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/53038264/how-to-get-a-the-sum-of-multiple-arrays-within-an-array-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!