我想在循环外访问数组变量。但它返回空值。下面是示例代码。
var result = [];
for (var i=0; i < 10; i++) {
result.push[i];
}
最佳答案
push 方法的语法是push()
而不是push[]
。
var result = [];
for (var i=0; i < 10; i++) {
result.push(i);
}
console.log(result);
有关push()
的更多信息,请查看How to append something to an array?
关于javascript - 在for循环外访问数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52852179/