最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using filter() rather than map() to modify an array of objects? - Stack Overflow

programmeradmin0浏览0评论

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?
发布评论

评论列表(0)

  1. 暂无评论