admin管理员组文章数量:1415460
I recently started learning javascript from a course on Educative. I have a very simple question on the difference between filter() and map(). I am given a list of student objects, each containing a name, sex, and an array of grades, and I am asked to return a list of female students containing their name, sex, and average grade. (Link: )
The input is:
const students = [
{
name: "Anna",
sex: "f",
grades: [4.5, 3.5, 4]
},
{
name: "Dennis",
sex: "m",
country: "Germany",
grades: [5, 1.5, 4]
},
{
name: "Martha",
sex: "f",
grades: [5, 4, 2.5, 3]
},
{
name: "Brock",
sex: "m",
grades: [4, 3, 2]
}
];
and the expected output is:
[ { name: 'Anna', sex: 'f', grades: 4 },
{ name: 'Martha', sex: 'f', grades: 3.625 } ]
The given solution is as follows. I understand everything except for the last line. Why are we using filter() rather than map() to apply avgGrade to the array of female students? I thought filter() was supposed to return only array elements that meet the conditions specified in a function.
Also, why does using filter(students,femaleList).map(avgGrade) not work?
function studentResult(students) {
const femaleList = student => student.sex === "f";
const avgGrade = arr => arr.grades = arr.grades.reduce((a,b) => a + b, 0) / arr.grades.length;
const filter = (student, func) => student.filter(func);
const result = filter(filter(students,femaleList),avgGrade);
return result;
}
const students = [
{
name: "Anna",
sex: "f",
grades: [4.5, 3.5, 4]
},
{
name: "Dennis",
sex: "m",
country: "Germany",
grades: [5, 1.5, 4]
},
{
name: "Martha",
sex: "f",
grades: [5, 4, 2.5, 3]
},
{
name: "Brock",
sex: "m",
grades: [4, 3, 2]
}
];
function studentResult(students) {
const femaleList = student => student.sex === "f";
const avgGrade = arr => arr.grades = arr.grades.reduce((a,b) => a + b, 0) / arr.grades.length;
const filter = (student, func) => student.filter(func);
const result = filter(filter(students,femaleList),avgGrade);
return result;
}
console.log(studentResult(students));
I recently started learning javascript from a course on Educative. I have a very simple question on the difference between filter() and map(). I am given a list of student objects, each containing a name, sex, and an array of grades, and I am asked to return a list of female students containing their name, sex, and average grade. (Link: https://www.educative.io/courses/the-plete-javascript-course-build-a-real-world-app-from-scratch/qVlyoEpxYEk)
The input is:
const students = [
{
name: "Anna",
sex: "f",
grades: [4.5, 3.5, 4]
},
{
name: "Dennis",
sex: "m",
country: "Germany",
grades: [5, 1.5, 4]
},
{
name: "Martha",
sex: "f",
grades: [5, 4, 2.5, 3]
},
{
name: "Brock",
sex: "m",
grades: [4, 3, 2]
}
];
and the expected output is:
[ { name: 'Anna', sex: 'f', grades: 4 },
{ name: 'Martha', sex: 'f', grades: 3.625 } ]
The given solution is as follows. I understand everything except for the last line. Why are we using filter() rather than map() to apply avgGrade to the array of female students? I thought filter() was supposed to return only array elements that meet the conditions specified in a function.
Also, why does using filter(students,femaleList).map(avgGrade) not work?
function studentResult(students) {
const femaleList = student => student.sex === "f";
const avgGrade = arr => arr.grades = arr.grades.reduce((a,b) => a + b, 0) / arr.grades.length;
const filter = (student, func) => student.filter(func);
const result = filter(filter(students,femaleList),avgGrade);
return result;
}
const students = [
{
name: "Anna",
sex: "f",
grades: [4.5, 3.5, 4]
},
{
name: "Dennis",
sex: "m",
country: "Germany",
grades: [5, 1.5, 4]
},
{
name: "Martha",
sex: "f",
grades: [5, 4, 2.5, 3]
},
{
name: "Brock",
sex: "m",
grades: [4, 3, 2]
}
];
function studentResult(students) {
const femaleList = student => student.sex === "f";
const avgGrade = arr => arr.grades = arr.grades.reduce((a,b) => a + b, 0) / arr.grades.length;
const filter = (student, func) => student.filter(func);
const result = filter(filter(students,femaleList),avgGrade);
return result;
}
console.log(studentResult(students));
Share
Improve this question
edited Apr 23, 2021 at 20:39
Liftoff
25.5k14 gold badges70 silver badges126 bronze badges
asked Apr 23, 2021 at 20:34
Amy LiAmy Li
411 silver badge3 bronze badges
3
- 2 Just an FYI seeing how you said you're just starting to learn, this code is written in a poor, confusing way and shouldn't be "aspired to". – Adam Jenkins Commented Apr 23, 2021 at 20:40
- 2 is this really the code from the course? ask your money back. – webduvet Commented Apr 23, 2021 at 20:48
-
@webduvet - OMG, I thought it was encountered in the wild, this is code used to teach people?
本文标签: javascriptUsing filter() rather than map() to modify an array of objectsStack Overflow
版权声明:本文标题:javascript - Using filter() rather than map() to modify an array of objects? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745181810a2646498.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论