过滤数组并返回搜索输入

问题描述 投票:0回答:1

我正在尝试过滤字符串数组的数组,并希望将包含搜索词的数组的所有实例推入其自己的数组中。

我尝试使用地图、过滤器、forEach 辅助方法,但似乎无法让搜索输入执行我想要执行的操作。

为了简单起见,我尝试在不使用传统的“for”循环的情况下执行此操作。

const booksArray = [
  [
    'The City We Became',
    'N. K. Jemisin',
    'fantasy',
    'fiction',
    'afrofutursim',
   'science fiction',
    'sci-fi'
  ],
  [
    'The Catcher in the Rye',
    'J. D. Salinger',
    'fiction',
    'young adult',
    'YA',
    'realism',
    'coming of age',
    'classic'
  ],
  [
    'The Hundred Thousand Kingdoms',
    'N. K. Jemisin',
    'fantasy',
    'fiction',
    'adventure',
    'series'
  ],
  [
    'Sapiens: A Brief History of Humankind',
    'Yuval Noah Harari',
    'nonfiction',
    'history',
    'anthropology',
    'science',
    'sociology'
  ],
  [
    'Behave: The Biology of Humans at Our Best and Worst',
    'Robert M. Sapolsky',
    'nonfiction',
    'anthropology',
    'science',
    'sociology',
    'biology'
  ],
  [
    'The Parable of the Talents',
    'Octavia Butler',
    'fiction',
    'dystopian',
    'science fiction'
  ],
  [
    '1984',
    'George Orwell',
    'fiction',
    'dystopian',
    'science fiction',
    'classics',
    'adult'
  ],
  [
    'Remarkably Bright Creatures',
    'Shelby Van Pelt',
    'fiction',
    'mystery',
    'magical realism'
  ],
  [
    'Crying in H Mart',
    'Michelle Zauner',
    'memoir',
    'nonfiction',
    'autobiography'
  ],
  [
    'Wild: From Lost to Found on the Pacific Crest Trail',
    'Cheryl Strayed',
    'nonfiction',
    'memoir',
    'adventure',
   'travel'
  ]
]

  const filterBooks = (searchInput) => { 
    let filteredBooks = []
    booksArray.filter(searchInput) 
        filteredBooks.push(booksArray)
  };


  console.log(filterBooks('fantasy')) 

// Uncaught TypeError: string "fantasy" is not a function
   at Array.filter (<anonymous>)
    at filterBooks (index.js:72:16)
    at index.js:78:15
javascript arrays function filter push
1个回答
0
投票

未正确使用过滤方法。过滤器方法需要一个回调函数,该函数应根据特定条件返回 true 或 false。但是,在提供的代码中,您将 searchInput 直接传递给过滤器方法,这是没有意义的。

您没有返回filterBooks函数的结果。因此,当您尝试使用 console.log 打印结果时,它将打印 undefined,因为该函数没有显式返回值。

代码调整:

const booksArray = [
  [
    'The City We Became',
    'N. K. Jemisin',
    'fantasy',
    'fiction',
    'afrofutursim',
   'science fiction',
    'sci-fi'
  ],
  [
    'The Catcher in the Rye',
    'J. D. Salinger',
    'fiction',
    'young adult',
    'YA',
    'realism',
    'coming of age',
    'classic'
  ],
  [
    'The Hundred Thousand Kingdoms',
    'N. K. Jemisin',
    'fantasy',
    'fiction',
    'adventure',
    'series'
  ],
  [
    'Sapiens: A Brief History of Humankind',
    'Yuval Noah Harari',
    'nonfiction',
    'history',
    'anthropology',
    'science',
    'sociology'
  ],
  [
    'Behave: The Biology of Humans at Our Best and Worst',
    'Robert M. Sapolsky',
    'nonfiction',
    'anthropology',
    'science',
    'sociology',
    'biology'
  ],
  [
    'The Parable of the Talents',
    'Octavia Butler',
    'fiction',
    'dystopian',
    'science fiction'
  ],
  [
    '1984',
    'George Orwell',
    'fiction',
    'dystopian',
    'science fiction',
    'classics',
    'adult'
  ],
  [
    'Remarkably Bright Creatures',
    'Shelby Van Pelt',
    'fiction',
    'mystery',
    'magical realism'
  ],
  [
    'Crying in H Mart',
    'Michelle Zauner',
    'memoir',
    'nonfiction',
    'autobiography'
  ],
  [
    'Wild: From Lost to Found on the Pacific Crest Trail',
    'Cheryl Strayed',
    'nonfiction',
    'memoir',
    'adventure',
   'travel'
  ]
]

const filterBooks = (searchInput) => { 
  const filteredBooks = booksArray.filter((book) => {
    return book.slice(2).includes(searchInput);
  });

  return filteredBooks;
};

console.log(filterBooks('fantasy')); 
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.