简体   繁体   English

无法推送到Array内部的Array

[英]Can't push to Array inside Array

var tags = new Array();
var tags[4]= new Array();
tags[4].push("Hello");

Somehow this doesnt work, console Says on line two that there's an unexpected Token... Can you help me somehow? 控制台不知何故不起作用,在第二行说有一个意外的令牌……您能以某种方式帮助我吗? Its an Array inside an Array. 它是一个数组内的一个数组。 I simplified the code, because the rest is right. 我简化了代码,因为其余的都是正确的。

Thx 谢谢

var tags[4] is incorrect. var tags[4]不正确。 Just tags[4] is needed. 仅需要tags[4]

It's a simple mistake to make. 这是一个简单的错误。 Just remove var from line 2... 只需从第2行删除var ...

var tags = new Array();
tags[4]= new Array();
tags[4].push("Hello");

tags[4] is already available by declaring tags on line 1. 通过在第1行声明tags ,即可使用tags[4]

Remove the var before tags[4] . tags[4]之前删除var tags[4] tags is the variable, tags[4] is a property of the object referenced by that variable, not another variable. tags是变量, tags[4]是该变量引用的对象的属性,而不是另一个变量。

var tags = new Array();
tags[4]= new Array();
tags[4].push("Hello");
var tags[4] // is incorrect.
// use this
tags[4]= new Array();
tags[4].push("Hello");

var keyword creates a variable so old value is lost . var关键字创建变量,因此旧值丢失。

The array tags has already been initialized, so you don't need var on the second line. 数组tags已被初始化,因此第二行不需要var Remove it and the code works as expected. 删除它,代码将按预期工作。

Remove var before tag[4] tag[4]前删除var tag[4]

Try this instead. 试试这个吧。

var tags = new Array();
tags[4]= new Array();
tags[4].push("Hello");

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

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