javascript数组格式通过for循环

javascript数组格式通过for循环,javascript,arrays,for-loop,Javascript,Arrays,For Loop,我做错了什么?我得到TypeError:items[I]作为错误未定义 var items = []; for(var i = 1; i <= 3; i++){ items[i].push('a', 'b', 'c'); } console.log(items); 您可以简单地使用以下内容: items.push(['a', 'b', 'c']); 无需使用索引访问数组,只需推入另一个数组即可 .push()方法将自动将其添加到数组的末尾 var items = []; for

我做错了什么?我得到
TypeError:items[I]作为错误未定义

var items = [];
for(var i = 1; i <= 3; i++){
    items[i].push('a', 'b', 'c');
}
console.log(items);

您可以简单地使用以下内容:

items.push(['a', 'b', 'c']);
无需使用索引访问数组,只需推入另一个数组即可

.push()
方法将自动将其添加到数组的末尾

var items = [];
for(var i = 1; i <= 3; i++){
    items.push(['a', 'b', 'c']);
}
console.log(items);
var项目=[];

对于(var i=1;我知道,这解决了我的问题!你能解释一下它为什么报告那个错误吗?我的意思是我的代码有什么问题吗?@DipendraGurung The
.push()
方法将自动将项附加到数组的末尾。在每次迭代中,您都试图访问数组中不存在的值。请参阅更新。您试图访问未定义的“项[i]”,因为数组为空。
var items = [];
for(var i = 1; i <= 3; i++){
    items.push(['a', 'b', 'c']);
}
console.log(items);
var items = [];
for(var i = 1; i <= 3; i++){
    items[i] = []; // Define the array so that you aren't pushing to an undefined object.
    items[i].push('a', 'b', 'c');
}
console.log(items);