Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
Nested filtering of 2D array doesn't work as expected

I have a 2D array where the outer array contains objects, each of which contains another array of objects. Here is the data:

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

I want to get the list of transactions that have their selected fields set to true. This is how I am filtering the data:

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

Instead of getting the transactions of only the first company, I'm getting the transactions of ALL companies, including the ones that are not selected. Am I making a mistake somewhere?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could flat-map the overall companies; then internally filter and map the transaction IDs and dates along with the company 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; }

Result

[
  { "companyId": 100, "transactionId": "10421", "transactionDate": "09/19/2022" },
  { "companyId": 100, "transactionId": "00467", "transactionDate": "05/08/2022" }
]
about 4 years ago · Juan Pablo Isaza Report

0

If youre just looking for the transactions you could use map, though it does return a second empty array. Or just add a little to your filter function to get only the company with what youre looking for.

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; }

Result

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
      }
    ]
  }
]
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!