簡體   English   中英

二維數組的嵌套過濾不能按預期工作

[英]Nested filtering of 2D array doesn't work as expected

我有一個二維數組,其中外部數組包含對象,每個對象都包含另一個對象數組。 這是數據:

const companies = [{
    companyId: 100,
    companyName: "Company 1",
    transactions: [
      { id: "10421", date: "09/19/2022", selected: true, },
      { id: "00467", date: "05/08/2022", selected: true, },
    ],
  },
  {
    companyId: 200,
    companyName: "Company 2",
    transactions: [
      { id: "259A6", date: "01/11/2022", selected: false, },
      { id: "87K37", date: "04/14/2022", selected: false, },
    ],
  },
];

我想獲取其selected字段設置為true的交易列表。 這就是我過濾數據的方式:

const selectedTransactions = companies.filter((c) =>
  c.transactions.filter((t) => t.selected === true));

我不是只獲得第一家公司的交易,而是獲得所有公司的交易,包括那些沒有被選中的公司。 我在某個地方犯了錯誤嗎?

您可以繪制整個公司的平面圖; 然后在內部過濾和映射交易 ID 和日期以及公司 ID。

 const companies = [{ companyId: 100, companyName: "Company 1", transactions: [ { id: "10421", date: "09/19/2022", selected: true }, { id: "00467", date: "05/08/2022", selected: true }, ], }, { companyId: 200, companyName: "Company 2", transactions: [ { id: "259A6", date: "01/11/2022", selected: false }, { id: "87K37", date: "04/14/2022", selected: false }, ], }]; const selected = companies .flatMap(({ companyId, transactions }) => transactions .filter(({ selected }) => selected) .map(({ id: transactionId, date: transactionDate }) => ({ companyId, transactionId, transactionDate }))); console.log(selected);
 .as-console-wrapper { top: 0; max-height: 100% !important; }

結果

[
  { "companyId": 100, "transactionId": "10421", "transactionDate": "09/19/2022" },
  { "companyId": 100, "transactionId": "00467", "transactionDate": "05/08/2022" }
]

如果您只是在尋找可以使用 map 的交易,盡管它確實返回了第二個空數組。 或者只是在您的過濾器功能中添加一點,以僅獲得您正在尋找的公司。

 const companies = [{ companyId: 100, companyName: "Company 1", transactions: [ { id: "10421", date: "09/19/2022", selected: true }, { id: "00467", date: "05/08/2022", selected: true }, ], }, { companyId: 200, companyName: "Company 2", transactions: [ { id: "259A6", date: "01/11/2022", selected: false }, { id: "87K37", date: "04/14/2022", selected: false }, ], }]; // USING MAP TO GET TRANSACTIONS const st = companies.map(c => c.transactions.filter(t => t.selected === true)) console.log(st); // USING FILTER TO GET COMPANY const selectedTransactions = companies.filter(c => { c.transactions = c.transactions.filter(t => t.selected === true); if (c.transactions.length) return c; }); console.log(selectedTransactions);
 .as-console-wrapper { top: 0; max-height: 100% !important; }

結果

1
[
  [
    {
      "id": "10421",
      "date": "09/19/2022",
      "selected": true
    },
    {
      "id": "00467",
      "date": "05/08/2022",
      "selected": true
    }
  ],
  []
]

2
[
  {
    "companyId": 100,
    "companyName": "Company 1",
    "transactions": [
      {
        "id": "10421",
        "date": "09/19/2022",
        "selected": true
      },
      {
        "id": "00467",
        "date": "05/08/2022",
        "selected": true
      }
    ]
  }
]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM