React by Patrik

Filtering Data by Multiple Criteria in JavaScript

Overview: Sometimes, filtering based on multiple attributes is necessary. This Snipp demonstrates how to combine different conditions in the filter() method to retrieve data that satisfies more than one criterion.

Implementation:

// Filter by gender and language
const femaleSpanishNames = namesWithAttributes.filter(person => person.gender === "female" && person.language === "Spanish");
console.log(femaleSpanishNames);
// Output: [{ name: "Manuela", gender: "female", language: "Spanish" }]

In this example, the array is filtered to include only objects where the gender is female and the language is Spanish. Using multiple conditions ensures that only the most relevant data is retrieved.

Comments