程序未在JavaScript中枚举

我正在尝试编写一个程序,该程序接受输入(库,authorName)并返回作者写的书的标题。

该库如下所示:

let library = [
  { author: 'Bill Gates',title: 'The Road Ahead',libraryID: 1254 },{ author: 'Carolann Camilo',title: 'Eyewitness',libraryID: 32456 },title: 'Cocky Marine',libraryID: 32457 }
];

我的代码如下:

let library = [
  { author: 'Bill Gates',libraryID: 32457 }
];

function searchBooks(library,author) {
  for (author in library) { //enumerate
    if (author in library) {
      let line = Object.values(library[0]) // find line of the wanted author
      let result = (line[0] + "," + line[1]) // store author name and title name
      return result
    } else {
      return "NOT FOUND"
    }
  }
}

console.log(searchBooks(library,'Bill Gates'))

输出看起来像这样: Bill Gates,The Road Ahead

问题: 无论我将向searchBook输入什么作者,它都会返回库的第一行Bill Gates。因此,我猜并不能一一列举。但是为什么不呢?

我首先想到也许我必须避免对[0]和[1]进行硬编码,而是使用i和i ++。但这似乎也不起作用,因为它随后输出TypeError: Cannot convert undefined or null to object

tzyyhjb 回答:程序未在JavaScript中枚举

几件事:

  1. for (author in library)正在查找库数组的每个键,而使用of来评估每个值。由于它是一个对象,因此我将该值命名为book
  2. 您的result可能有很多值,因此最好使其成为某种数组或集合,您可以在其中堆叠响应
  3. if (author in library)检查您的作者是否是原始库数组中的键,这也是不希望的。您真的想看看author是否是对象的值。更具体地说,您希望您的作者是对象的作者键的值。所以book.author == author
  4. 您的结果是一个数组,所以用换行符将值连接起来;如果结果数组中没有项目,则将为空字符串,这是一个falsey值。在这种情况下,您可以返回NOT FOUND消息。否则,您要退回所有书籍

let library = [
  { author: 'Bill Gates',title: 'The Road Ahead',libraryID: 1254 },{ author: 'Carolann Camilo',title: 'Eyewitness',libraryID: 32456 },title: 'Cocky Marine',libraryID: 32457 }
];

function searchBooks(library,author) {
  let result = []

  for (let book of library) {
    if (book.author == author) {
      let line = Object.values(book)       // find line of the wanted author
      result.push(line[0] + "," + line[1]) // store author name and title name
    }
  }

  return result.join('\n') || 'NOT FOUND'
}

console.log(1,searchBooks(library,'Carolann Camilo'))
console.log(2,'Bill Gates'))
console.log(3,'Oh boy'))

注意:

  • 有关避免使用Object.values的方法,请参见this answer
  • 有关仅使用所需书籍(使用filter)遍历库的方法,请寻找this answer
  • 有关另一种遍历库的方法(使用forEach),请寻找this answer
,

您可能要使用Array.filtersee MDN):

const library = [
  { author: 'Bill Gates',libraryID: 32457 }
];

console.log(findAuthor(library,`Bill Gates`));

function findAuthor(library,author) {
  return library.filter(book => book.author === author);
}

,

这是您的方法的有效版本:

let library = [
    { author: 'Bill Gates',author) {
    for (const book of library) { //enumerate
        if (book.author === author) {
            let result = (book.author + "," + book.title) // store author name and title name
            return result
        }
    }
    return 'NOT FOUND'
}

console.log(searchBooks(library,'Bill Gates'))

  • 首先,迭代book中的library,而不是作者。

  • 然后找出author中的book是否等于您要查找的author

  • 然后输出该书的authortitle,并用逗号分隔。

还请注意,这只会返回第一个匹配项,而不是所有匹配项。

,

您可以使用forEach遍历元素并将它们添加到给定结果中,以用于同一作者的多个条目

let library = [
  {author: 'Bill Gates',libraryID: 32457 }
];
function search(library,author) {
  let result = ""
  library.forEach(l => {
    if (l.author === author) {
      result += l.author+","+l.title+"\n"
    }
  })
  return result
}

console.log(search(library,"Carolann Camilo"))
本文链接:https://www.f2er.com/2654035.html

大家都在问