在对象数组中搜索值的函数

Search function for a value in an array of objects

我试图创建一个函数来搜索书籍对象数组中的书名。出于某种原因,我的代码无法正常工作,我已尝试按逻辑执行每个步骤,在我看来它应该可以正常工作。

let book = {
  title: "The 48 Laws of Power",
  author: "Robert Greene",
  numOfPages: 452,
  publisher: "Penguin Books",
  rating: "4.5 stars"
}

let book2 = {
  title: "How to Catch a Turkey",
  author: "Adam Wallace",
  numOfPages: 40,
  publisher: "",
  rating: "5 stars"
}

let book3 = {
  title: "Glitter Every Day: 365 Quotes from Women I Love",
  author: "Andy Cohen",
  numOfPages: 384,
  publisher: "Henry Holt and Co.",
  rating: "3 stars"
}

let bookArr = [book, book2, book3];
let searchBtn = document.getElementById('search-button');
let found = false;

let bookSearch = function() {
  found = false;
  let input = document.getElementById('book-search');
  for (i = 0; i < bookArr.length; i++) {
    if (input.value.toLowerCase == bookArr[i].title.toLowerCase) {
      found = true;
      break;
    }

  }
  if (found) {
    console.log(bookArr[i]);
  } else {
    console.log("The book was not found.");
  }
}
searchBtn.addEventListener('click', bookSearch);
<input type="text" id="book-search" placeholder="Search books...">
<button id="search-button">Search</button>
<script src="object.js"></script>

最后,我是这个论坛的新手,也是编程的新手,所以如果这是一种错误的提问方式,我深表歉意,但我非常感谢对我的代码提出批评以及我应该在哪些方面做得更好。谢谢!

您可以使用 Array.find(),它非常简单:

var book = bookArr.find(book => book.title.toLowerCase() == input.value.toLowerCase())

工作演示

let book = {
  title: "The 48 Laws of Power",
  author: "Robert Greene",
  numOfPages: 452,
  publisher: "Penguin Books",
  rating: "4.5 stars"
}

let book2 = {
  title: "How to Catch a Turkey",
  author: "Adam Wallace",
  numOfPages: 40,
  publisher: "",
  rating: "5 stars"
}

let book3 = {
  title: "Glitter Every Day: 365 Quotes from Women I Love",
  author: "Andy Cohen",
  numOfPages: 384,
  publisher: "Henry Holt and Co.",
  rating: "3 stars"
}

let bookArr = [book, book2, book3];
let searchBtn = document.getElementById('search-button');
let found = false;

let bookSearch = function() {
  let input = document.getElementById('book-search');
  var book = bookArr.find(book => book.title.toLowerCase() == input.value.toLowerCase())

  if (book) {
    console.log(book);
  } else {
    console.log("The book was not found.");
  }
}
searchBtn.addEventListener('click', bookSearch);
<input type="text" id="book-search" placeholder="Search books...">
<button id="search-button">Search</button>
<script src="object.js"></script>

如果您只需要第一个匹配项,请使用 Array.find()。

books.find(book => book.title == 'mysearch');

如果你想要一组匹配项,请使用 Array.filter()。

books.filter(book => book.title == 'mysearch');

如果您使用的是 Typescript,那么您可以添加类型安全并使搜索功能更易于使用。

function search<Type, Key extends keyof Type>(arr: Type[], prop: Key, match: Type[Key]){
    // For a single item
    return arr.find(item => item[prop] == match);

    // For an array of matches
    return arr.filter(item => item[prop] == match);
}

然后我们为一本书制作一个界面,我们可以在自动完成的同时轻松搜索它。

interface Book {
    title: string;
    author: string;
    numOfPages: number;
    publisher: string
    rating: string;
}

search(books, "title", "Harry Potter")

这是一个 TS 游乐场,您可以自己尝试代码。 Link