Javascript多维数组语法错误

时间:2015-11-01 09:51:10

标签: javascript arrays multidimensional-array

<script type="text/javascript">
    var personnel = new Array();

    var personnel[0] = new Array();
    personnel[0][0] = "Name0";
    personnel[0][1] = "Age0";
    personnel[0][2] = "Address0";

    var personnel[1] = new Array();

    personnel[1][0] = "Name1";
    personnel[1][1] = "Age1";
    personnel[1][2] = "Address1";

    document.write("Name:" + personnel[1][0]);
</script>

当它进入浏览器时,我有这个错误:

  

SyntaxError:missing;在声明var personnel[0] = new Array();

之前

2 个答案:

答案 0 :(得分:3)

  

var personnel[0] = new Array();是语法错误!

     

您可以使用[]代替new Array()

来清理代码

试试这个:

&#13;
&#13;
var personnel = new Array();

personnel[0] = new Array();
personnel[0][0] = "Name0";
personnel[0][1] = "Age0";
personnel[0][2] = "Address0";

personnel[1] = new Array();

personnel[1][0] = "Name1";
personnel[1][1] = "Age1";
personnel[1][2] = "Address1";

document.write("Name:" + personnel[1][0]);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

还有很多方法可以创建数组。

例如

// creates the same array but with a lot less typing.
var personnel = [   // I have added a new line and then indented the nested arrays
    ["Name0","Age0","Address0"],   // adds an array in side the first
    ["Name1","Age1","Address1"],   // you separate all the items in the array with commas
];   //close the array.

有些人喜欢将其扩展到更远的地方,如

var personnel = [   // always put the first opening braket here
    [
        "Name0",  // again indenting so it is easy to see where you are at
        "Age0",
        "Address0", // Javascript used not let you have a comma on the last 
                    // element. Some people still think that should be enforced 
                    // and some IDE will report it as an error. It is up to you.
    ],[   // close the first and open the second array
        "Name1",
        "Age1",
        "Address1"
    ],   
];   //close the array.

然后在一行中非常紧凑。

var personnel = [["Name0","Age0","Address0"],["Name1","Age1","Address1"]];

您可以将数组插入数组

var person0 = ["Name0","Age0","Address0"];
var person1 = ["Name1","Age1","Address1"];
var personnel = [person0, person1]; //the two arrays inserted into another array;

var personnel = []; // create an empty array;
personnel[0] = person0; // puts person0 array as the first item
personnel[1] = person1; // and the second array as the second item

我个人最喜欢使用分割字符串并创建数组的String.split方法。

"Name0,Age0,Address0".split(","); // splits the string were there are ',' commas
// creates the array ["Name0","Age0","Address0"];

分隔符可以是任何

"Name0 Age0 Address0".split(" "); //in this case a space is used

甚至可以分开单词

"Name0 banana Age0 banana Address0".split(" banana "); // splits at space banana space

使用split创建嵌套数组非常简单。如果您有要列入阵列的长列表,则很方便。

var personnel = [
       "Name0,Age0,Address0".split(","),
       "Name1,Age1,Address1".split(","),
];

所有这些技术都创建了完全相同的数组,还有更多方法可以实现。没有正确或错误的方式,所以选择适合你的方式和快乐的编码。

改善您的风格不仅可以更轻松,更快速地编写代码,还可以更轻松地找到错误和错误。

BTW脚本标记不需要type="text/javascript"所需的全部<script>以及代码</script>之后的结束标记,类型默认为text / javascript