How to remove nested arrays based on element position in JavaScript

I’m working with a nested array structure that looks like this:

var data = [
  [3, 7, 5, 2],
  [8, 7, 9, 1],
  [2, 4, 9, 3],
  [3, 4, 8, 3]
];

I need to filter out sub-arrays that contain a particular value at a specific index. For instance, if I want to exclude all sub-arrays where the element at index 1 equals ‘7’, my expected output would be:

var data = [
  [2, 4, 9, 3],
  [3, 4, 8, 3]
];

Similarly, filtering out sub-arrays with ‘9’ at index 2 should give me:

var data = [
  [3, 7, 5, 2],
  [3, 4, 8, 3]
];

What’s the most efficient approach to accomplish this filtering? I’m dealing with large datasets containing over 1000 sub-arrays, so performance is a concern.

You could also use array destructuring for more complex filtering. Like data.filter(([, second]) => second !== 7) to filter by index 1, or data.filter(([,, third]) => third !== 9) for index 2. Makes it way clearer what you’re targeting.

I use this a lot when certain positions in my data actually mean something specific. Performance is the same as regular filter, but it’s much easier to read - especially with multiple conditions or when you’re looking at old code months later.

For big datasets, a plain for loop usually beats filter. I use let result = []; for(let i = 0; i < data.length; i++) { if(data[i][targetIndex] !== targetValue) result.push(data[i]); } and it’s noticeably faster with thousands of elements. Filter’s function call overhead really adds up on large arrays. I’m seeing 15-20% better performance with 10k+ sub-arrays in my tests. But if you care more about readable code than speed, filter works fine.

totally! using the filter method like data.filter(arr => arr[index] !== value) is super efficient. it works fast even for large datasets. js built-in functions make this kind of task easy!