该问题采用非负整数和字符串列表并返回新的数字数组。
const filterlist = [1, 2, "a", "b", 0];
function filter_list(l) {
let arrayResult = [];
for (const iteml of l) {
if (parseInt(iteml)) {
arrayResult.push(iteml);
}
}
return arrayResult;
}
const res = filter_list(filterlist);
for (let i = 0; i < res.length; i++) {
console.log(res[i]);
}
// console.log(parseInt(0));
这样做时,结果发现只解析了 1 和 2,而 0 仍然悬而未决,但是为什么当我将转换带到控制台(注释行)时,它显示这是一个数字。我知道零是一个错误值,但是我该如何绕过它呢?