最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to filter an array in array of objects in Javascript? - Stack Overflow

programmeradmin0浏览0评论
 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!

 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!

Share Improve this question asked Sep 7, 2021 at 16:42 blackroxblackrox 2312 gold badges4 silver badges17 bronze badges 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. – Mike 'Pomax' Kamermans 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
 |  Show 1 more comment

2 Answers 2

Reset to default 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);

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

发布评论

评论列表(0)

  1. 暂无评论