【问题标题】:Need help returning an array with two arrays: borrowed books and returned books需要帮助返回一个包含两个数组的数组:借书和还书
【发布时间】:2021-07-22 00:04:40
【问题描述】:

我试图返回一个包含两个数组的数组,第一个是借来的书,另一个是归还的书。我必须使用的提示是:

public/src/books.js 中的partitionBooksByBorrowedStatus() 函数只有一个参数:

  • 一系列书籍。

它返回一个包含两个数组的数组。所有输入的书籍都存在于第一个或第二个数组中。

第一个数组包含已借出但尚未归还的图书,而第二个数组包含已归还的图书。您可以检查是否归还通过查看borrows 数组中的第一笔交易来查看状态。

以下是提供的部分数据:

const books = [{
id: "5f447132d487bd81da01e25e",
title: "sit eiusmod occaecat eu magna",
genre: "Science",
authorId: 8,
borrows: [
  {
    id: "5f446f2e2cfa3e1d234679b9",
    returned: false,
  },
  {
    id: "5f446f2ed3609b719568a415",
    returned: true,
  },
  {
    id: "5f446f2e1c71888e2233621e",
    returned: true,
  },
  {
    id: "5f446f2e6059326d9feb9a68",
    returned: true,
  },
  {
    id: "5f446f2ede05a0b1e3394d8b",
    returned: true,
  },
  {
    id: "5f446f2e4081699cdc6a2735",
    returned: true,
  },
  {
    id: "5f446f2e3900dfec59489477",
    returned: true,
  },
  {
    id: "5f446f2e6059326d9feb9a68",
    returned: true,
  },
  {
    id: "5f446f2e409f8883af2955dd",
    returned: true,
  },
  {
    id: "5f446f2e3900dfec59489477",
    returned: true,
  },
  {
    id: "5f446f2eae901a82e0259947",
    returned: true,
  },
  {
    id: "5f446f2ef2ab5f5a9f60c4f2",
    returned: true,
  },
  {
    id: "5f446f2ea6b68cf6f85f6e28",
    returned: true,
  },
  {
    id: "5f446f2eed18105706d6ca19",
    returned: true,
  },
  {
    id: "5f446f2eae901a82e0259947",
    returned: true,
  },
  {
    id: "5f446f2e91c2af00cb74e82b",
    returned: true,
  },
  {
    id: "5f446f2e5aa2bb5545a0f8a6",
    returned: true,
  },
  {
    id: "5f446f2ea508b6a99c3e42c6",
    returned: true,
  },
  {
    id: "5f446f2e50cc2da9cd80efdb",
    returned: true,
  },
  {
    id: "5f446f2e0b3e2ff72fc503e7",
    returned: true,
  },
  {
    id: "5f446f2e91c2af00cb74e82b",
    returned: true,
  },
  {
    id: "5f446f2ef795e593cd3cd19d",
    returned: true,
  },
  {
    id: "5f446f2e2f35653fa80bf490",
    returned: true,
  },

这是我迄今为止尝试过的:

function partitionBooksByBorrowedStatus(books) {

  let available = [];
  let unavailable = [];
  const bookStatuses = [];

  books.forEach((book) => {
   const isBookReturned = book.borrows[0].returned;
   if (isBookReturned) { // if book is returned
     available.push(book);
   } else { // if book is not returned
     unavailable.push(book);
   }
 });

 bookStatuses.push(available);
 bookStatuses.push(unavailable);

 return bookStatuses; //

}

当我运行代码时,它会返回一个错误,指出“预期 3 等于 6”,我这辈子都想不通。非常感谢您的 cmets 和建议,谢谢!

【问题讨论】:

  • 你是如何调用函数的? return 不会导致错误。预期的输出是多少?
  • 有点不清楚36 指的是什么。但是您返回的数组似乎是向后的;问题陈述表明应该首先签出书籍(可能不可用)。
  • 您可能必须生成 minimal reproducible example,因为正如 Dave 所说,据我所知,您不会从该数据中获得 3 或 6。
  • 我的错戴夫是正确的,我的返回值颠倒了,我将 available.push(book) 换成了不可用.push(book) 并修复了它。谢谢大家!

标签: javascript


【解决方案1】:

感谢@Dave Newton,他提到我返回的数组似乎是向后的,我能够交换返回值,所以现在正确的代码是:

function partitionBooksByBorrowedStatus(books) {
let available = [];
let unavailable = [];
const bookStatuses = [];
books.forEach((book) => {
  const isBookReturned = book.borrows[0].returned;

if (isBookReturned) { // if book is not returned
  unavailable.push(book);
} else { // if book is returned
  available.push(book);
}
});
bookStatuses.push(available);
bookStatuses.push(unavailable);
return bookStatuses;
}

【讨论】:

    【解决方案2】:

    这可以通过 JavaScript 的 Array reduce 方法轻松完成。您需要为初始累加器提供一个映射预期输出的对象,然后根据图书是否至少出现一次未返回的图书来填充两个数组之一。 (也可以通过测试“returned == false”的个数是否为奇数)。

    假设 1:一本从未被借过的书应该算作“被归还” 假设 2:“书”是 ISBN 的字面上唯一的个人副本。

    更正:没有看到借用数组的第一个元素指示当前状态。

    const books = [
    {
        id: "0",
        title: "never borrowed",
        genre: "Science",
        authorId: 8,
        borrows: []
    },
    {
        id: "1",
        title: "returned",
        genre: "Science",
        authorId: 8,
        borrows: [
            {
                id: "5f446f2e2f35653fa80bf490",
                returned: true,
            }
        ]
    },
    {
        id: "5f447132d487bd81da01e25e",
        title: "not returned",
        genre: "Science",
        authorId: 8,
        borrows: [
            {
                id: "5f446f2e2cfa3e1d234679b9",
                returned: false,
            }
        ]
    }]
    
    function partitionBooksByBorrowedStatus(b) {
        return b.reduce( (a, c) => { a[+(c.borrows[0] && c.borrows[0].returned)].push(c); return a }, [[],[]] )
    }
    
    console.log(partitionBooksByBorrowedStatus(books))

    【讨论】:

    • 没有一元就足够钝了,而且效率低于简单明了的方法——不需要迭代整个borrows数组。 IMO 这里的嵌套过滤器/减少只是混淆了意图并使其更难推理。
    • 问题中没有任何明确的内容表明 borrows 数组的第一个元素是唯一一个 returned 可能为假的元素,因此从表面上看,您确实需要迭代整件事。
    • "您可以通过查看借贷数组中的第一笔交易来检查退货状态。"
    • 啊,我的错。让事情变得更加简单。
    • ? 是的,它有点隐藏在问题中(可能是为什么 OP 首先错过了这个问题;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 2012-05-23
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多