这是我编写的用于从数组中过滤掉数值的代码,但它返回完整的数组。我无法找出我的代码中的问题。请帮助我,我被困住了......
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>html demo</title>
</head>
<body>
<script>
arr = ["apple", 5, "Mango", 6];
function filterNumeric(arrayName){
var i = 0;
var numericArray=[];
for (i; i <arrayName.length;i++){
if (typeof(arrayName[i] === 'number')) {
numericArray+=arrayName[i];
}
}
return numericArray;
}
var filter = filterNumeric(arr);
alert(filter);
</script>
</body>
</html>
最佳答案
typeof
检查中的拼写错误:
if (typeof(arrayName[i]) === 'number') {
// ^^^ close the parentheses here
// ^^^ not there
关于javascript - 使用javascript从数组中过滤掉数字索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25864991/