JS嵌套数组 [英] JS nested arrays

查看:512
本文介绍了JS嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,
林试图在JS嵌套数组

Hey, Im trying to make a nested array in JS

    var lines = new Array(
                    "0"= new Array(
                                0['time']="10:00:00",
                                0['user']="User1",
                                0['content']="Line1",
                                ),
                    "1"= new Array(
                                1['time']="20:00:00",
                                1['user']="User2",
                                1['content']="Line2",
                                ),
                    "2"= new Array(
                                2['time']="30:00:00",
                                2['user']="User3",
                                2['content']="Line3",
                                ),
                    );

Chrome的调试器告诉我的),在第一嵌套数组的结尾是一个意外的标记

Chrome's debugger tells me the ), at the end of the first nested array is an "Unexpected Token"

推荐答案

从您的code,它看起来像你想在JavaScript中使用PHP风格的数组。 JavaScript数组不工作像PHP数组。这里的东西是更可能的工作:

From your code it looks like you're trying to use PHP-style arrays in JavaScript. JavaScript arrays don't work like PHP arrays. Here's something that's more likely to work:

var lines = [
  { time:     "10:00:00",
    user:     "User1",
    content:  "Line1"
  },
  { time:     "20:00:00",
    user:     "User2",
    content:  "Line3"
  },
  { time:     "30:00:00",
    user:     "User3",
    content:  "Line3"
  }
];

要进一步解释利特勒,在JavaScript中创建一个新的数组是这样的:

To explain a littler further, in JavaScript you create a new array like this:

var myArray = [ 5, 10, 15 ];

方括号( [] )表示数组和逗号()独立的开始和结束阵列中的每个元素。然后,要访问数组的元素,我们会做这样的事:

The square brackets ([]) denote the beginning and end of the array and commas (,) separate each element in the array. Then, to access the elements of the array, we would do something like this:

alert( myArray[0] );

...这将使5(第一个,或第0的数组元素)。

...which would give "5" (the first, or "0th," element in the array).

现在,而PHP有关联数组(阵列('A'=> 1,...)),在JavaScript中没有关联数组相反,您使用的对象常量,像这样的:

Now, whereas PHP has the associative array (array('a' => 1, ...)), in JavaScript there's no "associative array"; rather, you use an "object literal," like this:

var myObject = { a: 5, b: 10, c: 15 };

这将创建属性的新对象(你可以把它们想象成键)命名为A,B和C。有访问的属性有两种方式:

This creates a new object with properties (you can think of them as keys) named "a," "b," and "c." There are two ways to access a property:

alert( myObject['b'] );
alert( myObject.b );

在这两种情况下,10(该值我们分配给属性B)将给予

In both cases, "10" (the value we assigned to property "b") would be given.

现在回到你的锻炼。你会看到,在这里,我们已经创建了一个阵列( [] ),并给它三个要素,其中每个对象文本( { } )。要访问,比方说,第一个元素的用户属性,我们这样做:

Now back to your exercise. You'll see that here we've created an array ([]) and given it three elements, each of which is an object literal ({}). To access, say, the "user" property of the first element, we would do this:

alert( lines[0].user ); // => "User1"

编辑:如果您想命名外数组中的元素,它必须改变,以对象文本,并且可以嵌套像这样:

If you want to name the elements of the outer array, it must be changed to an object literal, and can be nested like so:

var lines = {
  one:   {  time:     "10:00:00",
            user:     "User1",
            content:  "Line1"
         },
  two:   {  ...
         },
         ...
};

我命名为清晰起见项目一,二和三,但你可以使用任何值请你。然而,如果你打算使用数字属性名 - 0,1,2,等等 - 在您的例子code,你不妨用其他的阵列,而不是对象。不像PHP,JavaScript的不允许在一个阵列差距。例如:

I've named the items "one," "two," and "three" for clarity's sake, but you can use any values you please. However, if you intend to use numeric property names--0, 1, 2, etc--as in your example code, you may as well use the other array rather than the object. Unlike PHP, JavaScript does not allow "gaps" in an array. For example:

var myArr = [1,2];
myArr[5] = 3;
alert( myArr ); // => [ 1, 2, undefined, undefined, undefined, 3 ];

这篇关于JS嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆