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

114
Vistas
Nested filter returning empty array

Below is the data that I am receiving and I am trying to filter so that a new array contains only objects with the desired location.

However, I'm running into an issue where my function is returning [], an empty array.

data:

[
  { data: [[Object], [Object], [Object]], id: 1 },
  { data: [[Object]], id: 2 },
  { data: [[Object], [Object], [Object], [Object]], id: 3 }
];

data[1]:

{"data": [{"name": "Joe", "job": "N/A", "location": "Los Angeles"}], "id": 2}

This is my current function:

const locations = ["Los Angeles", "Chicago"];
...
const filteredData = data.filter((i) =>
    i.data.filter((j) => locations.includes(j.location)),
);
return filteredData;

What is wrong and how can I fix this and get it filtering correctly?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

In the callback you pass to the Array.filter(), you need to return a boolean value to filter the array. If you do not return anything, the filter returns an empty array.

But in your case, you are returning inner filtered array that returns at least an empty array and the outer filter behaves it as a true value. So the outer filter will return all of the items in the original array. (not an empty one as you stated)

Also you are returning filteredData in a place where it results in a syntax error.

const data = [
    {"data": [{"name": "Joe", "job": "N/A", "location": "Los Angeles"}], "id": 2},
    {"data": [{"name": "Jane", "job": "N/A", "location": "Charlotte"}], "id": 3},
 ]
const locations = ["Los Angeles", "Chicago"];

const filteredData = data.filter((i) =>
    i.data.filter((j) => locations.includes(j.location)).length > 0,
);
console.log(filteredData); 

about 4 years ago · Juan Pablo Isaza Denunciar

0

Another Option is use some() to get your expected result. This way you don't need to loop through all item in data array comparing to filter()

const data = [
  { data: [{ name: "Joe", job: "N/A", location: "Los Angeles" }], id: 2 },
  { data: [{ name: "Jane", job: "N/A", location: "Charlotte" }], id: 3 },
  { data: [{ name: "Sam", job: "N/A", location: "SSS" }], id: 4 },
  {
    data: [
      { name: "John", job: "N/A", location: "AAA" },
      { name: "Doe", job: "N/A", location: "BBB" },
    ],
    id: 5,
  },
];
const locations = ["Los Angeles", "Chicago", "AAA"];

const existData = data.filter(el =>
  el.data.some(item => locations.includes(item.location))
);

console.log(existData);

If you also want to filter the data array, you can do like below.

const data = [
  { data: [{ name: "Joe", job: "N/A", location: "Los Angeles" }], id: 2 },
  { data: [{ name: "Jane", job: "N/A", location: "Charlotte" }], id: 3 },
  { data: [{ name: "Sam", job: "N/A", location: "SSS" }], id: 4 },
  {
    data: [
      { name: "John", job: "N/A", location: "AAA" },
      { name: "Doe", job: "N/A", location: "BBB" },
    ],
    id: 5,
  },
];
const locations = ["Los Angeles", "Chicago", "AAA"];

const filteredData = data.reduce((acc, cur) => {
  const filteredItem = cur.data.filter(item => locations.includes(item.location));
  if (filteredItem.length) {
    acc.push({ ...cur, data: filteredItem });
  }
  return acc;
}, []);

console.log(filteredData);

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