[英]JavaScript: Loop through Array of Objects and Find Sum of Specific Key Using Reduce Function
I have the following object below. 我下面有以下对象。 It contains an array of objects.
它包含一个对象数组。 Each object is a superhero that contains details about the superhero.
每个对象都是一个超级英雄,其中包含有关超级英雄的详细信息。
const superheroes = [
{
name: 'Bruce Wayne',
alias: 'Batman',
powerLevel: 50,
universe: 'DC Comics',
race: 'Human',
},
{
name: 'Wade Wilson',
alias: 'Deadpool',
powerLevel: 90,
universe: 'Marvel Comics',
race: 'Mutant',
},
{
name: 'Peter Parker',
alias: 'Spiderman',
powerLevel: 70,
universe: 'Marvel Comics',
race: 'Human',
},
{
name: 'Kristin Wells',
alias: 'Superwoman',
powerLevel: 99,
universe: 'DC Comics',
race: 'Kryptonian',
},
{
name: 'Barry Allen',
alias: 'The Flash',
powerLevel: 80,
universe: 'DC Comics',
race: 'Human',
},
{
name: 'Diana Prince',
alias: 'Wonder Woman',
powerLevel: 90,
universe: 'DC Comics',
race: 'Human',
},
{
name: 'Ororo Munroe',
alias: 'Storm',
powerLevel: 85,
universe: 'Marvel Comics',
race: 'Mutant',
},
{
name: 'Helen Parr',
alias: 'Elastigirl',
powerLevel: 70,
universe: 'Dark Horse Comics',
race: 'Human',
},
];
I want to find the average power level of all the superheroes. 我想找到所有超级英雄的平均功率水平。
I tried the code below: 我尝试了下面的代码:
function powerLevelAverage(obj){
let sum = 0;
let objCount = 0;
return obj.reduce((accumulator, element) => {
objCount += 1;
console.log(objCount)
sum = accumulator + element.powerLevel
return sum;
}, 0)
return Math.round(sum/objCount);
}
const averagePowerLevel = powerLevelAverage(superheroes);
averagePowerLevel
The code appears to return the sum of 634 but does not return the average. 该代码似乎返回634的总和,但不返回平均值。 What am I doing wrong?
我究竟做错了什么?
Note: I want to use reduce modifying function to solve this problem. 注意:我想使用减少修改功能来解决此问题。
There are three mistakes: 有三个错误:
return
statements in the code. return
语句。 So the code will not reach to the second one which gets average. reduce()
in a wrong way. reduce()
。 You are not using accumulator
anywhere. accumulator
。 You are using reduce()
like forEach()
forEach()
这样的reduce()
forEach()
reduce()
you already have the length
of array in form of obj.length
reduce()
计算每个对象,您已经具有obj.length
形式的数组length
A cleaner code can be. 可以使用更干净的代码。
function powerLevelAverage(obj){
return Math.round(obj.reduce((ac,a) => ac + a.powerLevel,0)/obj.length)
}
const superheroes = [ { name: 'Bruce Wayne', alias: 'Batman', powerLevel: 50, universe: 'DC Comics', race: 'Human', }, { name: 'Wade Wilson', alias: 'Deadpool', powerLevel: 90, universe: 'Marvel Comics', race: 'Mutant', }, { name: 'Peter Parker', alias: 'Spiderman', powerLevel: 70, universe: 'Marvel Comics', race: 'Human', }, { name: 'Kristin Wells', alias: 'Superwoman', powerLevel: 99, universe: 'DC Comics', race: 'Kryptonian', }, { name: 'Barry Allen', alias: 'The Flash', powerLevel: 80, universe: 'DC Comics', race: 'Human', }, { name: 'Diana Prince', alias: 'Wonder Woman', powerLevel: 90, universe: 'DC Comics', race: 'Human', }, { name: 'Ororo Munroe', alias: 'Storm', powerLevel: 85, universe: 'Marvel Comics', race: 'Mutant', }, { name: 'Helen Parr', alias: 'Elastigirl', powerLevel: 70, universe: 'Dark Horse Comics', race: 'Human', }, ]; function powerLevelAverage(obj){ return Math.round(obj.reduce((ac,a) => ac + a.powerLevel,0)/obj.length) } console.log(powerLevelAverage(superheroes))
You're returning your sum right here, which doesn't account for your division.... 您正要在这里退还您的款项,这不算您的部门...。
Change this to 更改为
function powerLevelAverage(obj){
let sum = 0;
let objCount = 0;
return obj.reduce((accumulator, element) => {
objCount += 1;
console.log(objCount)
sum = accumulator + element.powerLevel
// return sum; // Just get rid of this
}, 0)
return Math.round(sum/objCount);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.