程序未在 JavaScript 中枚举

Program does not enumerate in JavaScript

我正在尝试编写一个接受输入(图书馆、作者姓名)和returns作者所写书籍标题的程序。

图书馆看起来像这样:

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

我的代码如下所示:

let library = [
  { author: 'Bill Gates', title: 'The Road Ahead', libraryID: 1254 },
  { author: 'Carolann Camilo', title: 'Eyewitness', libraryID: 32456 },
  { author: 'Carolann Camilo', title: 'Cocky Marine', 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吧returns比尔·盖茨,图书馆的第一行。因此,我猜它没有列举。但为什么不呢?

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

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

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

function searchBooks(library, 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'))

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

  • 然后求bookauthor是否等于你要找的author

  • 然后输出那本书的authortitle,用逗号隔开

另请注意,这只是 returns 第一场比赛,并非所有比赛。

您可能想要使用 Array.filter (see MDN):

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

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

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

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

let library = [
  {author: 'Bill Gates', title: 'The Road Ahead', libraryID: 1254 },
  { author: 'Carolann Camilo', title: 'Eyewitness', libraryID: 32456 },
  { author: 'Carolann Camilo', title: 'Cocky Marine', 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"))

几件事:

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

let library = [
  { author: 'Bill Gates', title: 'The Road Ahead', libraryID: 1254 },
  { author: 'Carolann Camilo', title: 'Eyewitness', libraryID: 32456 },
  { author: 'Carolann Camilo', 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, searchBooks(library, 'Bill Gates'))
console.log(3, searchBooks(library, 'Oh boy'))

注:

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