微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

过滤多个数组并将它们中的对象添加到单个数组中

如何解决过滤多个数组并将它们中的对象添加到单个数组中

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",{
        id: "5f446f2e6059326d9feb9a68",{
        id: "5f446f2ede05a0b1e3394d8b",{
        id: "5f446f2e4081699cdc6a2735",{
        id: "5f446f2e3900dfec59489477",{
        id: "5f446f2e409f8883af2955dd",{
        id: "5f446f2eae901a82e0259947",{
        id: "5f446f2ef2ab5f5a9f60c4f2",{
        id: "5f446f2ea6b68cf6f85f6e28",{
        id: "5f446f2eed18105706d6ca19",{
        id: "5f446f2e91c2af00cb74e82b",{
        id: "5f446f2e5aa2bb5545a0f8a6",{
        id: "5f446f2ea508b6a99c3e42c6",{
        id: "5f446f2e50cc2da9cd80efdb",{
        id: "5f446f2e0b3e2ff72fc503e7",{
        id: "5f446f2ef795e593cd3cd19d",{
        id: "5f446f2e2f35653fa80bf490",{
        id: "5f446f2e7b9cd304fed3a8bc",{
        id: "5f446f2ed9aac23c0340aab2",],

authors 数组中的对象示例:

    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,authors);
  [
    {
      id: "5f447132320b4bc16f950076",title: "est voluptate nisi",genre: "Classics",authorId: 12,author: {
        id: 12,name: {
          first: "Chrystal",last: "Lester",borrows: [
        {
          id: "5f446f2e6059326d9feb9a68",...
      ],]

这是我目前所拥有的:

function getBooksPossessedByAccount(account,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;
    }

解决方法

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

由于作者信息需要作为属性添加到 book 对象,因此您不应将其推送到 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;
}
,

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

function getBooksPossessedByAccount(account,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
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。