I'm trying to remove all the numbers from an array. The loop works exactly as I want, but the splice() method still skips some elements for some reason.
let arr = ['1', '2', '3', '4', '5', 'notanumber', '6'];
for (let element of arr) {
let index = arr.indexOf(element);
let check = isNaN(element);
if (check === false) {
arr.splice(index, 1);
};
};
console.log(arr);
Expected output is: ['notanumber']
Current output is: ['2', '4', 'notanumber']
This would do
arr = arr.filter(x => isNaN(x));
The problem with calling splice for each element is that you're changing the array while iterating over it.
To do what filter does by hand the approach should be more like:
let wp = 0;
for (let i=0; i<arr.length; i++) {
if (isNaN(arr[i])) arr[wp++] = arr[i];
}
arr.length = wp;
In other words you keep a "write pointer" wp and for each element of the array that you want to keep you move it in place, without moving subsequent elements that still need to be processed. Only at the end you resize the result. This is much faster (o(N) instead of o(N²)) and simpler to keep correct.
This second approach moreover will mutate the array inplace, filter instead will return a new array and this is different if other references to the array are present in the program.
Use this as it uses a filter which is less code than a loop:
let arr = ['1', '2', '3', '4', '5', 'notanumber', '6'];
console.log(arr.filter(x => isNaN(x)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With