function arrayAnalyzer(numbers){
var array = [];
for(var i = 0; i < arguments.length; i++) {
array.push(arguments.charAt(i));
}
console.log(array);
}
arrayAnalyzer(7, -3, 0, 12, 44, -5, 3);
它给我的“arguments.charAt”无效
最佳答案
只需推送参数[i]
:
function arrayAnalyzer(numbers) {
var array = [];
for(var i = 0; i < arguments.length; i++) {
array.push(arguments[i]);
}
console.log(array);
}
arrayAnalyzer(7, -3, 0, 12, 44, -5, 3);
您也可以用更短的方式做同样的事情:
function arrayAnalyzer(numbers) {
// Since arguments is Array-like object let's convert it to array:
return Array.prototype.slice.call(arguments);
}
console.log(arrayAnalyzer(7, -3, 0, 12, 44, -5, 3));
顺便说一句,charAt
是 method of String prototype和 arguments
是 Array-like object当然不是字符串。
关于javascript - 无法使用循环中的参数字符填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31493785/