5

I got an array like this:

let arr = ['1','2','0','3','0',undefined,'0',undefined,'3','',''];

In order to filter the 'undefined' and '' element of this array and convert it to a number, I do like this:

arr = arr.filter(function(e){
    if(e){
        return parseInt(e);
    }
});

I got:

[1,2,3,3]

The 0 has also been filtered because 'return 0' means 'return false';

I want to know how do you normally do with this kind of problem?

Update:

I saw the following answers propose many useful ways. I learned a lot from it.

And if the element in the array need to be a number, not a string, should I traverse the array again? Or there is an alternative one-step method?

1
  • You mean "number" or "integer"? Commented Jan 12, 2018 at 7:28

7 Answers 7

4

Replace

return parseInt(e);

with

return !isNaN(e);

Demo

var arr = ['1','2','0','3','0',undefined,'0',undefined,'3','',''];
var output = arr.filter(function(e){
    if(e){
        return !isNaN(e);
    }
});
console.log( output );

Edit

For converting the values to Number as well, just add .map( Number ) to the filter output

Demo

var arr = ['1','2','0','3','0',undefined,'0',undefined,'3','',''];
var output = arr.filter(function(e){
    if(e){
        return !isNaN(e);
    }
}).map( Number );
console.log( output );

Sign up to request clarification or add additional context in comments.

8 Comments

!isNaN(e); It can't filter the empty string.
@zhaozhiming It does, please check the demo as well.
if(e){ filters empty strings, not !isNaN
@KoshVery I meant that my answer is filtering out empty strings, not !isNaN alone, which was already happening for OP.
Sorry for misunderstanding! and you forgot the conversion to numbers...
|
3

You could chain the wanted conditions.

let array = ['1', '2', '0', '3', '0', undefined, '0', undefined, '3', '', ''];

array = array
    .filter(Boolean) // get only truthy values
    .map(Number);    // convert all values to number
    
console.log(array);

Comments

3

It's easy with array chain-functions:

let arr = ['1', '2', '0', '3', '0', undefined, '0', undefined, '3', '', '']
  // filter valid elements (undefined and '' evaluates to false)
  .filter(e => e)
  // to exclude '0' from the list, do a bitwise-or
  //.filter(e => e|0)
  // cast into numbers
  .map(e => e * 1);

console.log(arr);

Comments

2

With slight change

let arr = ['1','2','0','3','0',undefined,'0',undefined,'3','',''];


arr = arr.filter(function(e){
    if( parseInt(e) >=0 ){
        return  e;
    }
});
console.log(arr);

1 Comment

thanks, the elements in the result array are strings, and I want them to be numbers, should I traverse the array again? Or is there an alternative one-step way?
0

let arr = ['1','2','0','3','0',undefined,'0',undefined,'3','',''];
let result = [];
for(let item of arr) {
  let num = +item;
  if(item === '' || Number.isNaN(num)) {
    continue;
  }
  result.push(num);
}
console.log(result);

Comments

0

You can use RegExp.prototype.test() with RegExp /^\d+$/ to check if the value is one or more digits

arr = arr.filter(n => /^\d$/.test(n))

Comments

0

let arr = ['1','2','0','3','0',undefined,'0',undefined,'3','',''];

// map everything to Number (undefined becomes NaN)
// filter all valid numbers
arr = arr.map(Number).filter(e => !isNaN(e))
console.log(arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.