新的JavaScript,因为我正在上第一堂课。在我们在课堂上做的类似代码的作业上获得错误。当我在终端中运行命令时出现此错误。我认为我的第一个块是正确的,但我可能是错的。这可能是我未经训练的眼睛看不到的东西。 感谢
var prices = ['100', '125', '129', '37', '38', '75', '87', '94', '300', '301',
'305', '50', '0.30', '0.01', '0.5', '5', '15', '24', '35', '1041', '1', '17',
'21', '28', '97', '6', '10', '49', '65', '89', '6', '10', '49', '65', '89'];
//convert string to integer
var numPricesArray = prices.join();
console.log("numPricesArray: " + numPricesArray);
//filter values under 25
var cutoffPrice = numPricesArray.filter(function(num){
return num < 25;
});
console.log(cutoffPrice);
答案 0 :(得分:2)
如评论中所述,调用prices.join()会返回一个字符串。具体来说,它将返回字符串&#34; 100,125,129&#34;等等。因为数组具有方法过滤器,而字符串不是,所以当您尝试对该字符串调用过滤器时,将引发错误。
来源: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter