Skip to content
Advertisement

How to remove nulls from array and a linked array whilst preserving order

I know how to remove null values from a single array using filters:

y = [7, 8, 9, null, null, 3, 4, null, 1]

y.filter(Number)
// [7, 8, 9, 3, 4, 1]

However, I’d also like to remove all those of an associated array whilst preserving the order of elements in both arrays.

Say I have,

x = [42, 60, 70, 100, 200, 400, 500, 900, 1000]

y = [7, 8, 9, null, null, 3, 4, null, 1]

where an element of x is associated with y, i.e., if y[94] is null (which should be removed), x[94] should be removed too.

The end result should be:

x = [42, 60, 70, 400, 500, 1000]

y = [7, 8, 9, 3, 4, 1]

I’ve tried finding what elements are null and then manually looping through them to remove the nulls, but I’d like a more elegant solution.

Cheers!

Advertisement

Answer

You can simply filter() the arrays against each other by index as provided as the second parameter in the callback.

const x = [42, 60, 70, 100, 200, 400, 500, 900, 1000]
const y = [7, 8, 9, null, null, 3, 4, null, 1]

const xFiltered = x.filter((n, i) => n !== null && y[i] !== null);
const yFiltered = y.filter((n, i) => n !== null && x[i] !== null);

console.log(...xFiltered)
console.log(...yFiltered)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement