π₯Filter Helper
The filter() method is a built-in array method in JavaScript that allows you to create a new array containing elements that pass a certain condition. It provides a clean and concise way to filter out elements from an array based on a specified criterion.
Use Case - 1: In the following example, finding students grade >= 90
const students = [
{ name: 'Quincy', grade: 96 },
{ name: 'Jason', grade: 84 },
{ name: 'Alexis', grade: 100 },
{ name: 'Sam', grade: 65 },
{ name: 'Katie', grade: 90 }
];
const studentGrades = students.filter(student => student.grade >= 90);
console.log(studentGrades);
Output :
[
{ name: 'Quincy', grade: 96 },
{ name: 'Alexis', grade: 100 },
{ name: 'Katie', grade: 90 }
]
Use Case - 2: In the following example, finding students grade >= 90
const songs = [
{ name: "Lucky You", duration: 4.34 },
{ name: "Just Like You", duration: 3.23 },
{ name: "The Search", duration: 2.33 },
{ name: "Old Town Road", duration: 1.43 },
{ name: "The Box", duration: 5.23 },
];
console.log(songs.filter((song) => song.duration > 3));
Output :
[
{ name: 'Lucky You', duration: 4.34 },
{ name: 'Just Like You', duration: 3.23 },
{ name: 'The Box', duration: 5.23 }
]
Last updated