javascript 数组中对象属性的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27879827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:07:25  来源:igfitidea点击:

Sum of object properties within an array

javascriptfor-loop

提问by Brandon

I have an array with properties, take the following for example:

我有一个带有属性的数组,例如以下内容:

var arrayPeople = [
    {
        name: 'joe',
        job: 'programmer',
        age: 25,
    },
    {
        name: 'bob',
        job: 'plumber',
        age: 30,
    };
];

I want to create a function that will return their average ages, here is what i have that is currently not working

我想创建一个函数来返回他们的平均年龄,这是我目前无法使用的

var ageAverage = function(array) {
    var age = 0, average;
    for (var i = 0; i < array.length; i++) {
        age += array[i].age;
    }
    average = age / array.length;
    return average;
};

What I am getting when running this function is Not A Number

运行此函数时我得到的不是数字

But what I can get to work is:

但我可以开始工作的是:

var ageAverage = function(array) {
    var age = 0, average;
    for (var i = 0; i < array.length; i++) {
        age = array[0].age;
    }
    average = age / array.length;
    return average;
};

The subtle difference here is that I took away the += and just made it =, and i took away the use of our variable i and just gave it the first index of the array. So in doing this, I know that the function is correctly substituting the parameter array with arrayPeople, but there is something going wrong in my for loop that is stopping i from becoming 0 and 1 (the indexes of arrayPeople).

这里的细微差别是我去掉了 += 并让它变成了 =,而我去掉了变量 i 的使用,只是给了它数组的第一个索引。因此,在这样做时,我知道该函数正确地用 arrayPeople 替换了参数数组,但是我的 for 循环中出了点问题,阻止了 i 变为 0 和 1(arrayPeople 的索引)。

P.S.I am trying to do this on my own, and I am not looking for someone to just do the problem for me, I would really appreciate just some hints on what to fix or maybe a concept I need to research the understand how to fix this problem.

PS我正在尝试自己做这件事,我不是在找人来为我解决问题,我真的很感激一些关于要修复什么的提示,或者我需要研究的概念,了解如何修复这个问题。

Thanks!!

谢谢!!

采纳答案by Alexander T.

You need initialize agevariable (var age = 0)

您需要初始化age变量 ( var age = 0)

var arrayPeople = [{
  name: 'joe',
  job: 'programmer',
  age: 25,
}, {
    name: 'bob',
    job: 'plumber',
    age: 30,
}];

var ageAverage = function(array) {
  var age = 0,
      average;

  for (var i = 0; i < array.length; i++) {
    age += array[i].age;
  }

  average = age / array.length;
  return average;
};

console.log(
  ageAverage(arrayPeople)
);

in your variant, agevariable equals undefined, and undefined += number(for example 25) returns NaN;

在您的变体中,age变量等于undefined,并且undefined += number(例如 25)返回NaN

also change ,to ;in this line

也更改,;在这一行

from

for (var i = 0, i < array.length; i++) {

to

for (var i = 0; i < array.length; i++) {

回答by CD..

Consider using mapand reduce:

考虑使用mapreduce

arrayPeople.
  map(function(item){ return item.age; }).
  reduce(function(a, b){ return a + b; }, 0) / arrayPeople.length;

Or just the reduceas @KooiInc pointed out:

或者就像reduce@KooiInc 指出的那样:

arrayPeople.reduce(function (a,b) { return a + b.age; }, 0) / arrayPeople.length;


A working Example:

一个工作示例:

var arrayPeople = [{
    name: 'joe',
    job: 'programmer',
    age: 25,
  }, {
    name: 'bob',
    job: 'plumber',
    age: 30,
  }],
  average = arrayPeople.reduce(function(a, b) {
    return a + b.age;
  }, 0) / arrayPeople.length;

document.write(average);

回答by Mouhamad Kawas

You should change the for to:

您应该将 for 更改为:

for (var i = 0; i < array.length; i++) instead of for (var i = 0, i < array.length; i++)

for (var i = 0; i < array.length; i++) 而不是 for (var i = 0, i < array.length; i++)

and give initial value to age

并赋予年龄初始值

var age=0

回答by Ricky

There's always a one-liner.

总有一个单线。

arrayPeople.reduce((a, o, i, p) => a + o.age / p.length, 0));

console.log(`${[{
    name: 'joe',
    job: 'programmer',
    age: 25,
  },
  {
    name: 'bob',
    job: 'plumber',
    age: 30,
  }
].reduce((a, o, i, p) => a + o.age / p.length, 0)}%`);