1 回答
TA贡献446条经验 获得超754个赞
var arr = [ '100px', 'abc'-6, [], -98765, 34, -2, 0, '300', , function(){alert(1);}, null, document, [], true, '200px'-30,'23.45元', 5, Number('abc'), function(){ alert(3); }, 'xyz'-90 ];
var finds = [];
for(var i=0;i<arr.length; i++) {
if(!isNaN(arr[i]) && typeof arr[i] === 'number') {
finds.push(arr[i]);
}
}
console.log('所有数字:' + JSON.stringify(finds));
finds = [];
var max = null, curNum;
for(var i=0;i<arr.length; i++) {
curNum = parseFloat(arr[i]);
if(!isNaN(curNum)) {
finds.push(arr[i]);
if(max===null || curNum>max) max = curNum;
}
}
console.log('可以转成数字:' + JSON.stringify(finds));
console.log('转成数字以后,最大值:' + max);
finds = [];
for(var i=0;i<arr.length; i++) {
if(isNaN(arr[i]) && typeof arr[i]==='number') {
finds.push(i);
}
}
console.log('NaN 所在的位置:' + finds.join(','));
添加回答
举报