Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
17
rated 0 times [  24] [ 7]  / answers: 1 / hits: 12536  / 10 Years ago, tue, june 3, 2014, 12:00:00

For an array: [5,something,,83,text,]



How to remove all non-numeric and empty values from an array? Desired output: [5,83]


More From » jquery

 Answers
11

Use array.filter() and a callback function that checks if a value is numeric:



var arr2 = arr.filter(function(el) {
return el.length && el==+el;
// more comprehensive: return !isNaN(parseFloat(el)) && isFinite(el);
});


array.filter has a polyfill for older browsers like IE8.


[#44845] Monday, June 2, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
annaliese

Total Points: 86
Total Questions: 97
Total Answers: 107

Location: Dominican Republic
Member since Sun, Sep 4, 2022
2 Years ago
;