Skip to content
Advertisement

How to declare an array in JS (like I’d do it in PHP)?

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”

Advertisement

Answer

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:

const 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:

const myArray = [ 5, 10, 15 ];

The square brackets ([]) denote the beginning and end of the array and commas (,) separate the elements of the array. Then, to access the elements, 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:

const 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:

const lines = {
  one: {
    time: '10:00:00',
    user: 'User1',
    content: 'Line1',
  },
  two: {
    // ...
  },
  // ...
};

I’ve named the items one, two, etc. 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—they’ll be filled with undefined. For example:

const myArr = [1, 2];
myArr[5] = 3;
alert( myArr ); // => [ 1, 2, undefined, undefined, undefined, 3 ];
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement