我正在尝试查找给定数组中女性的平均值
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;
}
答案 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
循环标题中也有错字,,
应该是;
。
答案 1 :(得分:2)
您可以将“女性”过滤掉到一个新的数组中,然后将其减少到所有年龄段的总数,然后使用女性数组的长度将总数除以:
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 :(得分:2)
您可以使用一个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>
答案 3 :(得分:1)
您需要检查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));
答案 4 :(得分:1)
您还可以:
.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);
答案 5 :(得分:0)
这是通过减少雌性年龄并除以雌性总数来实现的一种方法:
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);
答案 6 :(得分:0)
这只是我的方法(仅在数组上循环一次),并且基于下一个用于逐步计算平均值的数学公式:
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));
答案 7 :(得分:0)
这是我的解决方案,我发现它更灵活:
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;
答案 8 :(得分:0)
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'))