I got an array like this:
let arr = ['1','2','0','3','0',undefined,'0',undefined,'3','',''];
To filter out the 'undefined' and '' element of this array and convert it to a number, I like it:
arr = arr.filter(function(e){
if(e){
return parseInt(e);
}
});
I got:
[1,2,3,3]
0 is also filtered because 'return 0' means 'return false';
I want to know how you usually do this with this problem?
Update:
I saw that the following answers offer many useful ways. I learned a lot from him.
And if the element in the array should be a number, not a string, should I cross the array again? Or is there an alternative one-step method?
source
share