简体   繁体   English

为什么将元素“推送”到 javascript 中的数组列表时出现错误?

[英]why I got error when "push" the element to array list in javascript?

I wrote the code, but I don't know where the mistake is?我写了代码,但我不知道错误在哪里?

I want to fill out the numberList array from 1 to 6 by for/loop and push.我想通过for/loop和push把numberList数组从1填到6。

let numberList = [];


for (let i = 0; i <= 6; i++) {
  numberList[i].push();
}
console.log(numberList);

Your current syntax is wrong .您当前的语法错误 The actual Array.prototyp.push() syntax is like below,实际的Array.prototyp.push()语法如下所示,

push(element0)
push(element0, element1)
push(element0, element1, ... , elementN)

Code: : If want to fill out the numberList array from 1 to 6 by for/loop and push, then start i from 1 not 0代码::如果想通过for/loop和push从1到6填充numberList数组,那么从1开始i不是0

 let numberList = []; for (let i = 1; i <= 6; i++) { numberList.push(i); } console.log(numberList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM