Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all the numbers from an array?

Tags:

javascript

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']

like image 432
Ilya Kozel Avatar asked Dec 22 '25 21:12

Ilya Kozel


2 Answers

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.

like image 167
6502 Avatar answered Dec 24 '25 09:12

6502


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)))
like image 33
VFDan Avatar answered Dec 24 '25 11:12

VFDan