繁体   English   中英

从嵌套在另一个对象数组中的对象数组中访问值

[英]accessing values from an array of objects nested within another array of objects

我正在尝试创建一个 function,它返回特定帐户 ID 创建“借用”记录的次数。 我目前拥有的代码如下所示,

function numberOfBorrows(account, books) {
  let borrows = 0;
  for (let i = 0; i < account.length; i++){
    let accountID = account[i].id;
    for (let j = 0; j < books.length; j++){
      let bookID = books[j].borrows;
      for (let h = 0; h < bookID.length; i++){
        let usersID = bookID[h].id;
        if (accountID === usersID) {
          borrows += 1;
        }
      }
    }
  }
  return borrows;
}

数据来自这里,

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",
  },
  {
    id: "5f446f2ed46724f41c9fc431",
    picture: "https://api.adorable.io/avatars/75/ferrell.morris@ecolight.com",
    age: 35,
    name: {
      first: "Ferrell",
      last: "Morris",
    },
    company: "ECOLIGHT",
    email: "ferrell.morris@ecolight.com",
    registered: "Thursday, February 8, 2018 1:16 PM",
  },
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,
      }}]

我完全不确定如何解决这个问题并遍历所有不同的嵌套数组,并且由于某种原因借用变量一直返回为 0。

一种选择是使用数组reduce方法来聚合 object 中的借用计数,由借用者的 ID 键入。 这个 object 可以在您将来需要任何人的借款数量时使用。

 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, } ] } ] const borrows = books.reduce((acc, el) => { el.borrows.forEach(borrow => { acc[borrow.id] = (acc[borrow.id] || 0) + 1; }); return acc; }, {}); console.log(borrows);

您可以使用 reduce 和 filter 执行以下操作,

 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", }, { id: "5f446f2ed46724f41c9fc431", picture: "https://api.adorable.io/avatars/75/ferrell.morris@ecolight.com", age: 35, name: { first: "Ferrell", last: "Morris", }, company: "ECOLIGHT", email: "ferrell.morris@ecolight.com", registered: "Thursday, February 8, 2018 1:16 PM", }]; 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, } ] } ] const numberOfBorrows = (account, books) => { const borrows = books.reduce((acc, curr) => { return acc + curr.borrows.filter(item => item.id === account).length; }, 0); return borrows; } const ret = accounts.map(item => { return {id: item.id, borrowCount: numberOfBorrows(item.id, books)}; }); console.log(ret);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM