You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助修正filterBooks函数:按搜索词精准过滤书籍

修复filterBooks函数实现精准书籍搜索

我需要编写filterBooks函数,要求接收搜索字符串与书籍列表,返回匹配搜索输入的书籍,且需使用flattenObjectValuesIntoArray函数搜索书籍对象的所有字段。当前实现的filterBooks函数在输入搜索词'fantasy'时返回了7个元素,但预期结果为2个,无法完成精准过滤,需要修正代码。


相关原始代码

书籍列表

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

原扁平化函数

// Flatten object keys into an array so that we search the entire object by the input value
 const flattenObjectValuesIntoArray = (arrOfObjs) => {
  let flattenedObj;
  const flattenedObjsArr = [];
  for (let obj = 0; obj < arrOfObjs.length; obj++) {
    const objValues = Object.values(arrOfObjs[obj]);
    flattenedObj = [...objValues.flat()]
    flattenedObjsArr.push(flattenedObj)
  }
 return flattenedObjsArr;
};

原filterBooks函数

// Filter books based on search input and render filtered books to the DOM
 let filterBooks = (books) => {
  let flattenedObj;
   const flattenedObjsArr = [];
   for (let obj = 0; obj < books.length; obj++) {
    const objValues = Object.values(books[obj]);
    flattenedObj = [...objValues.flat()]
    flattenedObjsArr.push(flattenedObj)
  }
  return flattenedObjsArr;
};

问题分析

  1. 原filterBooks函数未接收搜索字符串参数,也未实现过滤逻辑,仅返回所有书籍的扁平化值数组,完全不符合需求。
  2. 原flattenObjectValuesIntoArray函数仅能处理对象数组,无法单独处理单个书籍对象,不利于遍历过滤时调用。

修正后的代码

调整扁平化函数(支持单个对象处理)

// 支持处理单个对象或对象数组,返回扁平化后的值数组
const flattenObjectValuesIntoArray = (input) => {
  // 处理对象数组
  if (Array.isArray(input)) {
    return input.map(obj => Object.values(obj).flat());
  }
  // 处理单个对象
  return Object.values(input).flat();
};

重新实现filterBooks函数

// 接收搜索词和书籍列表,返回匹配的书籍对象
const filterBooks = (searchTerm, books) => {
  // 统一转为小写,实现大小写不敏感搜索
  const lowerSearchTerm = searchTerm.toLowerCase();
  
  return books.filter(book => {
    // 扁平化当前书籍的所有字段值
    const flattenedValues = flattenObjectValuesIntoArray(book);
    // 检查是否有值匹配搜索词(同步转小写)
    return flattenedValues.some(value => 
      value.toLowerCase().includes(lowerSearchTerm)
    );
  });
};

验证结果

调用filterBooks('fantasy', books)会返回包含"fantasy"标签的2本书籍,符合预期:

[
  {
    title: "The City We Became",
    author: "N. K. Jemisin",
    tags: ["fantasy", "fiction", "afrofutursim", "science fiction", "sci-fi"]
  },
  {
    title: "The Hundred Thousand Kingdoms",
    author: "N. K. Jemisin",
    tags: ["fantasy", "fiction", "adventure", "series"]
  }
]

内容的提问来源于stack exchange,提问作者Anthony Luth Jr.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 12:03:16