DEV Community

Cover image for JavaScript’s Filter Function Explained By Applying To College

JavaScript’s Filter Function Explained By Applying To College

Kevin Kononenko on June 03, 2018

If you are familiar with the college application process, then you can understand JavaScript’s filter functions. Compared to the map() and reduce(...
Collapse
 
bojanorter profile image
bojanorter • Edited

For filtering like this,

let admitted = students.filter(function(student){
   return student.gpa > 3.2 && student.sat > 1900;
})

i would use arrow functions new in ECMAScript 6.

then this becomes

let admitted = students.filter(student => 
student.gpa > 3.2 && student.sat > 1900
})

Other than that, I liked your post.

Collapse
 
kbk0125 profile image
Kevin Kononenko

Yeah, it is always tough for me to decide which version to use, maybe I just need to start including the second version in comments :)

Collapse
 
kamalhm profile image
Kamal • Edited

this is nice! Thanks!

edit

you should fix the typo in the student attributes, because it's case sensitive

Collapse
 
kbk0125 profile image
Kevin Kononenko

Thanks Kamal! which one are you talking about?

Collapse
 
iksworks profile image
Ishmael Sunday • Edited

Some of the names start with caps and other don’t. They all should start lower or uppercase for consistency.

Also in your explanation, you said they need 3.2 or up. The function is not considering 3.2. You used (>) which excludes 3.2. Should it be >= ? You might want to as “s” word student in the return. students.gpa etc.

I am just a beginner. Starting my first JS class on the 18th. Thanks for the article too.

Thread Thread
 
kbk0125 profile image
Kevin Kononenko

Hey Ishmael, yep, you are correct about the 3.2 thing. As for "students" v. "Students", I tried to use the uppercase when referring to actual students, and lowercase when referring to the students array.

My original article uses interactive images that make this a little more clear:

blog.codeanalogies.com/2018/05/14/...

Collapse
 
kamalhm profile image
Kamal

Instead of student.sat you should change it to student.SAT

Thread Thread
 
kbk0125 profile image
Kevin Kononenko

Good point. Fixed the SAT issues.

Collapse
 
cyberfly profile image
Muhammad Fathur Rahman

thanks great sharing

Collapse
 
delimanicolas profile image
Nicolas Lima

Nice article!