对于数组:["5","something","","83","text",""]
如何从数组中删除所有非数字和空值?期望的输出:["5","83"]
最佳答案
使用array.filter()
和一个回调函数 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适用于 IE8 等较旧的浏览器。
关于Javascript/jQuery : remove all non-numeric values from array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24022682/