Javascript/jQuery : remove all non-numeric values from array

标签 javascript jquery arrays

对于数组:["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/

相关文章:

javascript - 使 ul-li 动态导航

jQuery,阻止表单提交输入但允许表单通过按钮单击提交

javascript - 如何在 V8 中优化访问时间的小型对象引用数组?

javascript - 是否可以强制用户的浏览器发出(并遵循)正文中包含 JSON 的 GET 请求?

javascript - AngularJS 指令运行时绑定(bind)

javascript - 如何用CSS下推整个页面

c++ - 如何声明指向另一个数组元素的指针数组

javascript - 为什么单击按钮时我的 jQuery 代码不起作用?

javascript - 使用 jquery 将事件附加到 future 元素

arrays - 类数组错误 - "Expressions are not allowed at the top level"