Nested collection filter with JavaScript

Filtering nested collections in JavaScript requires searching through objects that contain arrays of other objects. Let's explore how to filter an array based on properties within nested objects.

The Problem

Suppose we have an array of nested objects where each object contains a legs array with carrier information:

const arr = [{
   id: 1,
   legs:[{
      carrierName:'Pegasus'
   }]
},
{
   id: 2,
   legs:[{
      carrierName: 'SunExpress'
   },
   {
      carrierName: 'SunExpress'
   }]
},
{
   id: 3,
   legs:[{
      carrierName: 'Pegasus'
   },
   {
      carrierName: 'SunExpress'
   }]
}];

console.log('Original array:', JSON.stringify(arr, null, 2));
Original array: [
  {
    "id": 1,
    "legs": [
      {
        "carrierName": "Pegasus"
      }
    ]
  },
  {
    "id": 2,
    "legs": [
      {
        "carrierName": "SunExpress"
      },
      {
        "carrierName": "SunExpress"
      }
    ]
  },
  {
    "id": 3,
    "legs": [
      {
        "carrierName": "Pegasus"
      },
      {
        "carrierName": "SunExpress"
      }
    ]
  }
]

Using Array.filter() with Array.some()

We need to filter objects that contain at least one leg with a specific carrier name. The some() method checks if any element in the nested array matches our condition:

const arr = [{
   id: 1,
   legs:[{
      carrierName:'Pegasus'
   }]
},
{
   id: 2,
   legs:[{
      carrierName: 'SunExpress'
   },
   {
      carrierName: 'SunExpress'
   }]
},
{
   id: 3,
   legs:[{
      carrierName: 'Pegasus'
   },
   {
      carrierName: 'SunExpress'
   }]
}];

// Simple approach: filter by single carrier name
function filterByCarrier(arr, carrierName) {
   return arr.filter(item => 
      item.legs.some(leg => leg.carrierName === carrierName)
   );
}

const pegasusFlights = filterByCarrier(arr, 'Pegasus');
console.log('Flights with Pegasus:', JSON.stringify(pegasusFlights, null, 2));
Flights with Pegasus: [
  {
    "id": 1,
    "legs": [
      {
        "carrierName": "Pegasus"
      }
    ]
  },
  {
    "id": 3,
    "legs": [
      {
        "carrierName": "Pegasus"
      },
      {
        "carrierName": "SunExpress"
      }
    ]
  }
]

Advanced Filtering with Multiple Criteria

For more complex filtering with multiple possible carrier names, we can use a more sophisticated approach:

const arr = [{
   id: 1,
   legs:[{
      carrierName:'Pegasus'
   }]
},
{
   id: 2,
   legs:[{
      carrierName: 'SunExpress'
   },
   {
      carrierName: 'SunExpress'
   }]
},
{
   id: 3,
   legs:[{
      carrierName: 'Pegasus'
   },
   {
      carrierName: 'SunExpress'
   }]
}];

const filterByKeys = (arr = [], keys = []) => {
   const keySet = new Set(keys);
   return arr.filter(item => 
      item.legs.some(leg => keySet.has(leg.carrierName))
   );
};

// Filter by multiple carriers
const keys = ['Pegasus'];
const result = filterByKeys(arr, keys);
console.log('Filtered result:', JSON.stringify(result, null, 2));
Filtered result: [
  {
    "id": 1,
    "legs": [
      {
        "carrierName": "Pegasus"
      }
    ]
  },
  {
    "id": 3,
    "legs": [
      {
        "carrierName": "Pegasus"
      },
      {
        "carrierName": "SunExpress"
      }
    ]
  }
]

Comparison of Approaches

Method Best For Performance
Simple filter with some() Single criterion Fast and readable
Set-based filtering Multiple criteria Optimal for large arrays

Conclusion

Use Array.filter() combined with Array.some() to filter nested collections efficiently. The some() method checks if any nested element matches your criteria, making it perfect for this type of filtering operation.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements