var arr = [] arr[0] = { "itemA": "A", "itemB": "B", "itemC": "C" } arr[1] = { "itemA": "A", "itemB": "B", "itemC": "C" }
I think you are trying to apply static language constructs to the dynamic and different world of Javascript. Javascript really has no concept of arrays in the sense that many languages ββdo.
In Javascript, an array simply represents a special type of object (a hash itself) that has a property of special length. The integer "indexes" that you see above (that is, 0 in arr [0]) just have a search. A property of special length is defined as one larger than the largest integer key. In my example above, arr.length is 2. But if I were to assign:
arr[100] = { "itemA": "A", "itemB": "B", "itemC": "C" }
Then arr.length will be 101, although I did nothing to assign any of the elements from 2 to 99.
Similarly, we usually donβt predefine objects such as structures in Javascript, and thus anonymous objects will pretty much do what you want (or use a documented factory function, for example, in Ken's example).
"new Array ()" is optional as a short "var a = [];" the syntax is faster. :)
source share