函数接受搜索字符串和书籍列表,并返回与搜索输入匹配的所有书籍 (STUCK)

Function takes in a search string and a list of books and returns all of the books that match the search input (STUCK)

提问人:Anthony Luth Jr. 提问时间:9/6/2023 最后编辑:deepanshu223Anthony Luth Jr. 更新时间:9/6/2023 访问量:133

问:

我被赋予了一项任务,我似乎无法成功完成,并且需要一些帮助来编写代码才能完成任务。任何帮助实现这一目标将不胜感激。

filterBooks 函数接受搜索字符串和书籍列表,并返回与搜索输入匹配的所有书籍。它应该使用 flattenObjectValuesIntoArray 函数轻松搜索 book 对象中的所有字段。

Books 对象和 flattenObjectValuesIntoArray 函数如下所述。我需要帮助找到一种方法,将与搜索输入匹配的书籍返回到数组中。

当我刚刚运行filterBooks函数试图使其适合给定的flattenObjectValuesIntoArray函数时,我收到返回的响应:

“给定过滤器'幻想',您的 filterBooks 函数返回一个带有 '7' 元素的数组,我们预计它会返回 '2'”

我不知道如何让它有效地缩小搜索范围以获得“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;
};
JavaScript 数组函数 对象 扁平化

评论

0赞 deepanshu223 9/6/2023
您尚未包含实际发生过滤器的部分,因此您将获得 7 本书,而不是预期的 2 本书。
0赞 Alexander Nenashev 9/6/2023
这回答了你的问题吗?用于筛选数组并返回数组中的值以匹配搜索输入的功能

答:

0赞 deepanshu223 9/6/2023 #1

假设我们在书籍属性中寻找完全匹配的文本,您可以按如下方式完成过滤功能。

let filterBooks = (books, filter) => { // Where filter is the string to be searched
  const results = [];
  let flattenedBooks = flattenObjectValuesIntoArray(books); // Call existing function
  for(let book = 0; book < books.length; book++){
    // Search in the flattened books array, also make lowercase to include case insensitive results
    if (flattenedBooks[book].map(e => e.toLowerCase()).includes(filter.toLowerCase())){
        results.push(books[book]);
    }
  }
  return results;
};

代码接收 book 对象和筛选器字符串,调用扁平化函数,并在每个扁平化 book 数组中搜索筛选器字符串,如果找到匹配项,则将 book(未扁平化)推送到结果数组中。

完整的代码如下所示

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

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

 let filterBooks = (books, filter) => { // Where filter is the string to be searched
  const results = [];
  let flattenedBooks = flattenObjectValuesIntoArray(books); // Call existing function
  for(let book = 0; book < books.length; book++){
    // Search in the flattened books array, also make lowercase to include case insensitive results
    if (flattenedBooks[book].map(e => e.toLowerCase()).includes(filter.toLowerCase())){
        results.push(books[book]);
    }
  }
  return results;
};

console.log(filterBooks(books,"fantasy"));

0赞 Brock Duerstock 9/6/2023 #2

首先,如果任务的目标是使用该方法以便于按搜索词进行过滤,那么您应该 中调用该方法,而不是复制相同的代码并对其进行更改😉。flattenObjectValuesIntoArrayfilterBooks

其次,方法调用的输入应该一次是一本书,而不是完整的书籍数组,这样输出的只是我们正在查看的特定书籍的字段(标题、作者和所有标签),而不是本书的所有字段。否则,我们就无法知道这片被压平的田地属于哪本书。flatten ObjectValuesIntoArray

为此,我们遍历每本书,利用该方法将本书的所有字段返回到单个数组中,我们可以再次遍历并比较任何匹配的字符串:flattenObjectValuesIntoArray

 // Filter books based on search input and render filtered books to the DOM
 let filterBooks = (searchTerm, books) => {
   let filteredBooks = [];

   for (let i = 0; i < books.length; i++) {
      // All field values from this book
      let flattenedBook = flattenObjectValuesIntoArray(books[i]);

      for (let j = 0; j < flattenedBook.length; j++) {
         // Check if search term matches any of this book's values
         if (flattenedBook[j] == searchTerm) {
            filteredBooks.push(book[i]);
            break; // No need to continue searching fields for this book 
         }
      }
   }

   return filteredBooks;
};