3
 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"]
   }
 ];

This is my array of books and I want to make a new array depending on the areas. For example if user clicks on button - Horror, then I want to load only books that have horror in their -areas-, Im newbie in JS and I cant find a right way to do it. I want to make a new array named filteredBooks. Thank you for your help!

6
  • Use the Array.includes() method.
    – Barmar
    Commented Sep 7, 2021 at 16:43
  • @Mike'Pomax'Kamermans I don't think that's a good duplicate for this. It's not searching for a value in a nested array.
    – Barmar
    Commented Sep 7, 2021 at 16:47
  • 1
    I'm sure there are duplicates of this, but I can't find any right now.
    – Barmar
    Commented Sep 7, 2021 at 16:48
  • @barmar the question title was misleading, the actual question itself was about the same "array with objects". not an actually nested array. Commented Sep 7, 2021 at 17:39
  • 1
    They're both about arrays of objects, but the comparison method for the property is completely different.
    – Barmar
    Commented Sep 7, 2021 at 17:41

2 Answers 2

11

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);

3

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

1
  • 1
    It's a bit easier to filter over the books and use some to check to see if areas includes the filter: return books.filter(val => filters.some(filter => val.areas.includes(filter))).
    – Andy
    Commented Sep 7, 2021 at 17:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.