求一组对象的平均值
Posted
技术标签:
【中文标题】求一组对象的平均值【英文标题】:find average of an array of objects 【发布时间】:2019-04-05 23:08:01 【问题描述】:我试图找出给定数组中女性的平均值
sortArray([
name:'Sarah', gender:'female', age:25,
name:'Tom', gender:'male', age:18,
name:'Tim', gender:'male', age:65,
name:'Kim', gender:'female', age:58
]);
这就是我下面的内容
function averageFemale(list)
let sum = 0;
let femaleCount = 2;
let avg = sum / femaleCount;
for (let i = 0, i < list.length; i++)
sum += list[i];
return avg;
【问题讨论】:
你想平均他们的年龄吗?因为如果是这样,那么你真的应该在你的函数中的某个时刻使用他们的年龄。 你正在尝试解决这个代码战争 kata :D codewars.com/kata/5aa1931311dbd9358b000823/train/javascript 【参考方案1】:您可以将“女性”过滤到一个新数组中,然后将其减少到所有年龄的总和,然后使用女性数组的长度除以总数:
const people = [name:'Sarah', gender:'female', age:25, name:'Tom', gender:'male', age:18, name:'Tim', gender:'male', age:65, name:'Kim', gender:'female', age:58];
const females = people.filter(person => person.gender === 'female');
const average = females.reduce((total, next) => total + next.age, 0) / females.length;
console.log(average);
【讨论】:
【参考方案2】:您还可以:
使用.filter()
过滤输入数组以仅获取具有gender
为female
的对象。
使用.reduce()
获取年龄总和。
通过将上述计算的总和除以过滤数组中的对象数来计算平均值。
演示:
let data = [
name:'Sarah', gender:'female', age:25,
name:'Tom', gender:'male', age:18,
name:'Tim', gender:'male', age:65,
name:'Kim', gender:'female', age:58
];
let filteredData = data.filter(( gender ) => gender == 'female'),
avg = filteredData.reduce((r, c) => r + c.age, 0) / filteredData.length;
console.log(avg);
【讨论】:
如果你想单行,那么你可以考虑在归约函数中使用 source-array 参数,并在累加之前将 c.age 除以数组长度。即:reduce((r,c,i,a)=> r+c.age/a.length,0)【参考方案3】:您需要在将年龄添加到sum
之前检查性别,并且还要增加femaleCount
而不是将其硬编码为2
。
也不需要每次循环都重新计算avg
,最后一次就可以了。
function averageFemale(list)
let sum = 0;
let femaleCount = 0;
for (let i = 0; i < list.length; i++)
if (list[i].gender == 'female')
sum += list[i].age;
femaleCount++;
if (femaleCount == 0)
return 0; // prevent division by 0
let avg = sum / femaleCount;
return avg;
console.log(averageFemale([name:'Sarah', gender:'female', age:25, name:'Tom', gender:'male', age:18, name:'Tim', gender:'male', age:65, name:'Kim', gender:'female', age:58]));
for
循环标头中还有一个错字,,
应该是 ;
。
【讨论】:
【参考方案4】:你可以用一个 reduce 来做到这一点
function averageBy(arr)
const total, count = arr.reduce((a, b) =>
if (b.gender === 'female')
a.total += b.age;
a.count++;
return a;
, total: 0, count: 0);
return total / count;
console.log(averageBy(arr));
<script>
const arr = [
name: 'Sarah',
gender: 'female',
age: 25
,
name: 'Tom',
gender: 'male',
age: 18
,
name: 'Tim',
gender: 'male',
age: 65
,
name: 'Kim',
gender: 'female',
age: 58
];
</script>
【讨论】:
【参考方案5】:const arr= [
name:'Sarah', gender:'female', age:25,
name:'Tom', gender:'male', age:18,
name:'Tim', gender:'male', age:65,
name:'Kim', gender:'female', age:58
]
function averageAgeByGender(array,gender)
const getGender = (elem) => elem.gender===gender
const average = (a,b, i, self)=> (a+b.age/self.length)
return array.filter(getGender).reduce(average,0)
console.log(averageAgeByGender(arr,'female'))
【讨论】:
【参考方案6】:您需要检查gender
属性并取和age
并增加一个计数器,因为您事先并不知道。
function averageFemale(list)
var sum = 0,
count = 0,
i;
for (i = 0; i < list.length; i++)
if (list[i].gender === 'female')
sum += list[i].age;
++count;
return sum / count;
var array = [ name: 'Sarah', gender: 'female', age: 25 , name: 'Tom', gender: 'male', age: 18 , name: 'Tim', gender: 'male', age: 65 , name: 'Kim', gender: 'female', age: 58 ];
console.log(averageFemale(array));
【讨论】:
【参考方案7】:这是一种通过减少女性年龄并除以女性总数的方法:
const data = [name:'Sarah', gender:'female', age:25, name:'Tom', gender:'male', age:18, name:'Tim', gender:'male', age:65, name:'Kim', gender:'female', age:58];
let count = 0;
const average = data.reduce((total, person) =>
if (person.gender === 'female')
total += person.age;
count++;
return total;
, 0) / count;
console.log(average);
【讨论】:
【参考方案8】:这只是我的方法(只在数组上循环一次),并且基于下一个用于逐步计算平均值的数学公式:
avgN = (x1 + ... + xN) / N = (x1 + ... + xN-1) / N + (xN / N)
= (x1 + ... + xN-1) * (N-1) / (N * (N-1)) + (xN / N)
= ((x1 + ... + xN-1) / (N-1)) * ((N-1) / N) + (xN / N)
= avgN-1 * (N-1) / N + (xN / N)
= ((N-1) avgN-1 + xN) / N
var sortArray = [
name:'Sarah', gender:'female', age:25,
name:'Tom', gender:'male', age:18,
name:'Tim', gender:'male', age:65,
name:'Kim', gender:'female', age:58
];
function averageFemale(list)
var count = 0;
return list.reduce((avg, personData) =>
if (personData.gender === 'female')
avg = (count * avg + personData.age) / (++count);
return avg;
,0);
console.log("Average age of females is: " + averageFemale(sortArray));
【讨论】:
【参考方案9】:这是我的解决方案,我觉得它更灵活:
function sumOfArrayWithParameter (array, parameter)
let sum = null;
if (array && array.length > 0 && typeof parameter === 'string')
sum = 0;
for (let e of array) if (e && e.hasOwnProperty(parameter)) sum += e[parameter];
return sum;
要获得平均值,您只需像这样使用它:
let sum = sumOfArrayWithParameter(array, 'age');
let avg = sum / array.length;
【讨论】:
以上是关于求一组对象的平均值的主要内容,如果未能解决你的问题,请参考以下文章
数据处理,用C++或者Java编写,求一组数的方差均值众数标准差中位数等
C语言利用函数调用求一组数的最大值,最小值,平均数。。。。。。急。。。。