JavaScript:使用对象的精简功能

时间:2019-05-31 16:11:52

标签: javascript object callback sum reduce

我在下面有以下对象。它包含一个对象数组。每个对象都是一个超级英雄,其中包含有关超级英雄的详细信息。

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

  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

该代码似乎返回634的总和,但不返回平均值。我究竟做错了什么?

注意:我想使用减少修改功能来解决此问题。

2 个答案:

答案 0 :(得分:2)

有三个错误:

  • 您在代码中有两个return语句。因此,代码将不会达到平均的第二个代码。
  • 您使用错误的方式使用reduce()。您没有在任何地方使用accumulator。您正在使用reduce()之类的forEach()
  • 您不需要计算reduce()中的每个对象,您已经拥有length形式的obj.length数组

可以使用更干净的代码。

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

答案 1 :(得分:-1)

您要在此处退还款项,这不算您的部门。...

更改为

a