Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 532
Diesal11 Avatar asked Oct 21 '10 06:10

Diesal11


People also ask

How do you declare an array in JavaScript?

Creating an Array Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.

How do we declare and initialize an array in JavaScript?

You can initialize an array with Array constructor syntax using new keyword. The Array constructor has following three forms. Syntax: var arrayName = new Array(); var arrayName = new Array(Number length); var arrayName = new Array(element1, element2, element3,...

What is array () in JavaScript?

The Array() constructor is used to create Array objects.


2 Answers

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 ];
like image 100
Jordan Running Avatar answered Oct 11 '22 17:10

Jordan Running


try this

var lines = [ {'time': 'the time', 'user': 'the user', 'content': 'the content'}, {'time': 'the time', 'user': 'the user', 'content': 'the content'}];
like image 25
tsurahman Avatar answered Oct 11 '22 15:10

tsurahman