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

110
Views
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 answers
Answer question

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 Report

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 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!