【问题标题】:filter through multiple arrays and add objects from them to a single array过滤多个数组并将它们中的对象添加到单个数组
【发布时间】:2021-04-05 22:33:08
【问题描述】:

accounts 数组中的对象示例:

const accounts = [
  {
    id: "5f446f2ecfaf0310387c9603",
    picture: "https://api.adorable.io/avatars/75/esther.tucker@zillacon.me",
    age: 25,
    name: {
      first: "Esther",
      last: "Tucker",
    },
    company: "ZILLACON",
    email: "esther.tucker@zillacon.me",
    registered: "Thursday, May 28, 2015 2:51 PM",
  },

books 数组中的对象示例:

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,
      },
      {
        id: "5f446f2e7b9cd304fed3a8bc",
        returned: true,
      },
      {
        id: "5f446f2ed9aac23c0340aab2",
        returned: true,
      },
    ],
  },

作者数组中的对象示例:

    const authors = [
  {
    id: 0,
    name: {
      first: "Lucia",
      last: "Moreno",
    },
  },
  {
    id: 1,
    name: {
      first: "Trisha",
      last: "Mathis",
    },
  },
  {
    id: 2,
    name: {
      first: "Arnold",
      last: "Marks",
    },
  },

我需要编写函数函数getBooksPossessedByAccount(account, books, authors) {},它执行以下操作:它返回一个书籍和作者数组,代表给定帐户当前已签出的所有书籍。 仔细看看下面的对象, 因为它不仅仅是书本对象;作者对象嵌入其中。 输出示例:

getBooksPossessedByAccount(account, books, authors);
  [
    {
      id: "5f447132320b4bc16f950076",
      title: "est voluptate nisi",
      genre: "Classics",
      authorId: 12,
      author: {
        id: 12,
        name: {
          first: "Chrystal",
          last: "Lester",
        },
      },
      borrows: [
        {
          id: "5f446f2e6059326d9feb9a68",
          returned: false,
        },
        ...
      ],
    },
  ]

这是我目前所拥有的:

function getBooksPossessedByAccount(account, books, authors) {
      const accId = account.id;
      const result = [];
      for (let idxBks = 0; idxBks < books.length; idxBks++) {
        if (
          books[idxBks].borrows.id === accId &&
          books[idxBks].borrows.returned === false
        ) {
          result.push(books[idxBks]);
        }
        for (let idxAuth = 0; idxAuth < authors.length; idxAuth++) {
          let authorIdx = authors[idxAuth];
          if (authorIdx.id === result.authorId) {
            return [result, { author: authorIdx }];
          }
        }
      }
      return result;
    }

【问题讨论】:

  • 你为什么只检查book.borrows[0]
  • 您似乎正试图让 SO 为您编写整个库应用程序。这是您关于处理同一组数据的第四个问题。
  • 如果你正在努力学习,似乎你没有得到它。你总是犯同样的错误,比如booksOut.authorIDbooksOut 是一个对象数组,所以authorID 是数组元素的属性,而不是数组本身。
  • 你不想push 任何东西,因为你的最终结果应该是一个对象,而不是一个数组。您想合并来自account 的某些属性和来自booksauthors 的元素。
  • woah woah.. 慢下来.. 我只有一个问题要问老兄.. 你把函数命名为getBooksPossessedByAccount 那么为什么你还有其他参数booksorders

标签: javascript arrays function javascript-objects


【解决方案1】:

您需要搜索所有borrows,而不仅仅是borrows[0]。您可以使用some() 方法来检查所有这些。

由于需要将作者信息作为属性添加到图书对象,因此不应将其推送到 booksOut 数组中。

function getBooksPossessedByAccount(account, books, authors) {
  const accId = account.id;
  const booksOut = books.filter(
    (book) => book.borrows.some(borrow => !borrow.returned && borrow.id === accId)
  );
  booksOut.forEach(book => book.author = authors.find(author => book.authorID == author.id))
  return booksOut;
}

【讨论】:

    【解决方案2】:

    使用some 应该可以解决问题..

    function getBooksPossessedByAccount(account, books, authors) {
      let borrowedBooks=books.filter(book=>
        book.some(borrow=>borrow.id===account.id)
      )
      return borrowedBooks //array of book objects
      //return borrowedBooks.map(book=>book.id) //to show array of book ids
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-25
      • 1970-01-01
      • 2013-01-24
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-21
      • 2016-08-18
      相关资源
      最近更新 更多