我需要從數組中刪除字符串,我有這個功能; 它進行一些隨機測試並返回結果。從數組中刪除隨機字符串,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)'。 –