How to Filter Array of Objects by Value in JavaScript
How to filter an array in array of objects in Javascript? - Stack Overflow
ES6: Should I use filter or forEach? Whats the performance cost? : javascript
JavaScript Filter Exercise (from Udemy)
Videos
If you're working with arrays of objects in JavaScript, you may find yourself needing to filter the array based on a specific value. Luckily, JavaScript provides us with a built-in method to do just that: Array.filter() . In this article, we'll explore how to to filter an array of objects by value.
Let's say we have an array of objects representing different people, with properties like "name", "age", and "occupation". We want to filter this array to only include people who are over the age of 30. Here's how we could do it:
const people = [
{ name: "Alice", age: 25, occupation: "Engineer" },
{ name: "Bob", age: 35, occupation: "Teacher" },
{ name: "Charlie", age: 42, occupation: "Doctor" },
{ name: "Dave", age: 28, occupation: "Writer" },
];
const filteredPeople = people.filter((person) => person.age > 30);
console.log(filteredPeople);
// Output: [{ name: "Bob", age: 35, occupation: "Teacher" }, { name: "Charlie", age: 42, occupation: "Doctor" }]Here, we're using the filter() method on the people array, and passing in a function as an argument. This function takes each object in the array as its argument (which we've named person), and returns a boolean value indicating whether that object should be included in the filtered array.
In this case, we're using a simple comparison to check if the person's age is greater than 30. If it is, the function returns true, and the person is included in the filtered array. If not, the function returns false, and the person is excluded.
This is just one example of how you can use Array.filter() to filter an array of objects by value. Depending on your specific use case, you may need to use more complex comparison logic or filter on different properties of the objects in the array. But with Array.filter( as your tool, you'll be able to filter your arrays with ease.
filter() -> uses a callback function the return value of which decides what will be returned in the filtered array. If return value is true, the item is included in the resultant array.
includes() -> searches for something in an array of items using == equality
const books = [{
id: "1",
title: "Book title",
areas: ["horror", "mystery"]
}, {
id: "2",
title: "Book title 2",
areas: ["friendship", "love", "history"]
},
{
id: "3",
title: "Book title 3",
areas: ["friendship", "scifi"]
}
];
const filterValue = "horror";
const filteredBooks = books.filter(val => val.areas.includes(filterValue));
console.log(filteredBooks);
Since there is already a great answer (by @Kirill Savik) for finding a book by a singular genre, I'll take this opportunity to expand on the given answer so that it can take in an array of genres from which to show books with at least one of these genres.
Take a look at this snippet:
const books = [
{
id: "1",
title: "Book title",
areas: ["horror", "mystery"]
},
{
id: "2",
title: "Book title 2",
areas: ["friendship", "love", "history"]
},
{
id: "2",
title: "Book title 3",
areas: ["friendship", "scifi"]
}
];
function filter_books(filters) {
const filteredBooks = [];
filters.forEach(filterValue => {
filteredBooks.push(...books.filter(val => val.areas.includes(filterValue)));
});
console.log(filteredBooks);
};
filter_books(["horror", "scifi"]); // Outputs all books which have one or more of these ^ genres