我需要从数组中删除字符串,我有这个功能; 它进行一些随机测试并返回结果。从数组中删除随机字符串,JavaScript
function filter_list(array) {
array.filter((elem) => typeof elem === "string");
return (array);
}
当我不回什么,我得到了一个未定义(明显),但是当我返回数组我得到这个:
"Expected: '[1, 2]', instead got: '[1, 2, \'a\', \'b\']'
Expected: '[1, 0, 15]', instead got: '[1, \'a\', \'b\', 0, 15]'
Expected: '[1, 2, 123]', instead got: '[1, 2, \'aasf\', \'1\', \'123\',
123]'
Expected: '[]', instead got: '[\'a\', \'b\', \'1\']'
Expected: '[1, 2]', instead got: '[1, 2, \'a\', \'b\']' "
如果你的数字不包括+/-无穷大,你可以使用'array.filter(Number.isFinite)'。如果你的数字都是整数,使用'array.filter(Number.isInteger)'。 –