Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

125
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda