-1

What kind of array is this? Can any one explain it to me? I mean I have never encountered the elements of an array initialized like this!

var helpText = [
          {'id': 'email', 'help': 'Your e-mail address'},
          {'id': 'name', 'help': 'Your full name'},
          {'id': 'age', 'help': 'Your age (you must be over 16)'}
        ];
keshlam
  • 7,931
  • 2
  • 19
  • 33
AL-zami
  • 8,902
  • 15
  • 71
  • 130
  • possible duplicate of [Event handlers inside a Javascript loop - need a closure?](http://stackoverflow.com/questions/341723/event-handlers-inside-a-javascript-loop-need-a-closure) – Pointy Feb 02 '14 at 16:21
  • 2
    It's just an array of objects; it's basic JavaScript syntax. The code won't work for reasons explained in the linked duplicate question. – Pointy Feb 02 '14 at 16:22
  • As far as I understand, he asks about the data type of the values in the array, so it doesn't matter if it works or not, in this context.. Hence my answer below – Poni Feb 02 '14 at 16:25
  • 2
    I think you should learn JavaScript. – Anko - inactive in protest Feb 02 '14 at 16:41

2 Answers2

2

This is an array of objects..

Poni
  • 11,061
  • 25
  • 80
  • 121
  • 1
    IMHO, a link to a google search result (not even a single page) is not quite enough to consider this a valid answer. – lucke84 Feb 02 '14 at 16:35
  • Well, if this link (which points to Google) is ever going to be invalid then... you get my point :) Anyway what exactly would you write in here? It's as simple as it gets: Array of objects. – Poni Feb 02 '14 at 16:46
  • Point is this is a comment not an answer. OP wants an explanation this answer is not enough self-contained to provide one. Please [edit] or it might get deleted. – Bleeding Fingers Feb 02 '14 at 17:04
  • I realize some people can't accept the fact that **some answeres ARE short**, and rather need some amount of bullshit (within the answer's body) in order to feel warm and comfortable with it.. Please, do something better with your time guys :) And no offence really :)) – Poni Feb 04 '14 at 20:34
0

In JS there is two ways to create a new array.

With an Array constructor:

var helpText = new Array();

or with an array initializer:

var helpText = [];

Both of these snippets create a new empty array. However, it's not recommended to use the Array constructor, since a missuse of its arguments may lead to unexpected results. You can read more about arguments at MDN: Array.

When using an array initializer, you can also add members to an array:

var arrOfNumbers = [0, 1, 2, 4];

Also creating an array with a single member containing 0 is possible, unlike when using Array constructor:

var arrOfNumbers = [0];
Teemu
  • 22,918
  • 7
  • 53
  • 106