Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda