程序未在JavaScript中枚举

问题描述 投票:3回答:4

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

该库看起来像这样:

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的作者是什么,它都会返回库的第一行Bill Gates。因此,我猜并不能一一列举。但是为什么不呢?

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

javascript enumeration
4个回答
2
投票

几件事:

  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 },
  { 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的方法,请参见this answer
  • 关于仅在想要的书上使用filter遍历库的方法,请寻求this answer
  • 关于迭代库的另一种方法(使用forEach,请寻找this answer

1
投票

您可能想使用Array.filtersee 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);
}

1
投票

您可以使用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
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.