views:

41

answers:

3

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's debugger tells me the ), at the end of the first nested array is an "Unexpected Token"

+7  A: 

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"
  }
];

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] );

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

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 };

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 );

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"

Edit: 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:   {  ...
         },
         ...
};

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 ];
Jordan
ahh, i thought i might have got my PHP and JS mixed up, what i get for trying to learn both at once. is it possible to name the keys for the parent arrays as well? (just curious)
Diesal11
Yes it is by doing a `var line = { "0": { time: "10:00:00", ... }, ... }`. It then becomes an object that you can iterate with `for (var i in lines)`
Salman A
Diesal11, I've added some clarification to my answer. In regard to Salman's comment, it's tricky iterating over an object's properties like this for two reasons: First, it may have properties other than the ones you intended, especially if you're using external libraries like jQuery; and second, the properties will not be ordered. If you want to iterate over them in a specific order you should use an array--Object is not an ordered data type.
Jordan
+2  A: 

try this

var lines = [ {'time': 'the time', 'user': 'the user', 'content': 'the content'}, {'time': 'the time', 'user': 'the user', 'content': 'the content'}];
tsurahman
A: 

a small, standard, 'eye' fix


var lines = [{
    'time': 'the time',
    'user': 'the user',
    'content': 'the content'
},
{
    'time': 'the time',
    'user': 'the user',
    'content': 'the content'
}];
Elad Karako