【问题标题】:Use advanced functions to create new array of objects使用高级函数创建新的对象数组
【发布时间】:2021-04-04 21:43:18
【问题描述】:

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,
      },
    ],
  },

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",
  },

我需要编写函数getBorrowersForBook() 来执行此操作:它应该从本书的borrows 键返回一个包含所有交易的数组。但是,每笔交易都应包含相关的账户信息和returned 密钥,如下所示: (它还应该将数组中的对象数量限制为 10。)

getBorrowersForBook(book, accounts);
  [
    {
      id: "5f446f2e4eff1030e7316861",
      returned: true,
      picture: "https://api.adorable.io/avatars/75/barber.waters@kegular.biz",
      age: 37,
      name: {
        first: "Barber",
        last: "Waters",
      },
      company: "KEGULAR",
      email: "barber.waters@kegular.biz",
      registered: "Tuesday, April 14, 2020 9:15 PM",
    },
    {
      id: "5f446f2ecc5c4787c403f844",
      returned: true,
      picture: "https://api.adorable.io/avatars/75/dyer.trevino@slax.io",
      age: 34,
      name: {
        first: "Dyer",
        last: "Trevino",
      },
      company: "SLAX",
      email: "dyer.trevino@slax.io",
      registered: "Saturday, August 1, 2015 8:13 PM",
    },
  ]

这是我目前所拥有的:

    function getBorrowersForBook(book, accounts) {
  const { name, company, email, registered } = accounts;
  const { borrows } = book;
  let resultArray = [];
  let resultObject = borrows.filter((borrow) => {
    if (borrow > 1) {
      resultArray.push({ borrows, name, company, email, registered });
    }
  });
  resultArray.sort((resultA, resultB) => (resultA > resultB ? -1 : 1));
  resultArray.splice(0, 10);
  return resultArray;
}

【问题讨论】:

    标签: javascript arrays function javascript-objects


    【解决方案1】:

    基于借款人的 ID 存在于 accounts 数组中的假设,我建议如下所示。

    我不确定您要按什么排序,但下面的代码假定您按公司升序排序;如果不是这种情况,您可以根据自己的需要进行调整。

    function getBorrowersForBook(book, accounts) {
      // `borrows` is a list of transactions, each of type { id: string, returned: true }
      const { borrows } = book;
    
      const borrowers = borrows.map(({ id, returned })=> {
        // find account that matches the borrower's ID
        const account = accounts.find(account => account.id === id);
    
        // return the matching account, along with the `returned` info
        return {
          ...account,
          returned,
        };
      });
    
      return borrowers
        .sort((borrowerA, borrowerB) => {
          const companyA = borrowerA.company;
          const companyB = borrowerB.company;
          return companyA.localeCompare(companyB);
        })
        .slice(0, 10);
    }
    

    【讨论】:

    • 感谢您的帮助。我以前没有见过 localeCompare(),但我会熟悉它。
    猜你喜欢
    • 2013-03-21
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2017-03-02
    • 1970-01-01
    相关资源
    最近更新 更多