Understanding JavaScript Arrays
javascriptweblog.wordpress.com
Understanding JavaScript Arrays
1–10 of 17 posts
Re: Understanding JavaScript Arrays
#2Re: Understanding JavaScript Arrays
#3 var myData = new Array;
myData['key1'] = 'value1';
myData['key2'] = 'value2';
myData['key3'] = 'value3';
myData.length; //0Re: Understanding JavaScript Arrays
#4I want to stress that the following will work, but get you in trouble. Javascript does not support associative arrays. var myData = new Array; myData['key1'] = 'value1'; myData['key2'] = 'value2'; myData['key3'] = 'value3'; myData.length; //0
The length property does not return the number of items in the array though. It returns the value of the key with the highest numerical value if parsed as a integer plus one.
Re: Understanding JavaScript Arrays
#5Re: Understanding JavaScript Arrays
#6I want to stress that the following will work, but get you in trouble. Javascript does not support associative arrays. var myData = new Array; myData['key1'] = 'value1'; myData['key2'] = 'value2'; myData['key3'] = 'value3'; myData.length; //0
I would say that JS does support associative arrays, since it is the fundamental data type behind objects and arrays. Arrays are basically associative arrays, since the index is converted into a string and used as a key. E.g. myData[7] is equivalent to myData["7"]. The length property does not return the number of items in the array though. It returns the value of the key with the highest numerical value if parsed as…
Re: Understanding JavaScript Arrays
#7 var a = ["banana", Math.min, 4, "apple"];
for (var i=a.length; i; i--) {
console.log(a[i]);
}Re: Understanding JavaScript Arrays
#8I want to stress that the following will work, but get you in trouble. Javascript does not support associative arrays. var myData = new Array; myData['key1'] = 'value1'; myData['key2'] = 'value2'; myData['key3'] = 'value3'; myData.length; //0
I would say that JS does support associative arrays, since it is the fundamental data type behind objects and arrays. Arrays are basically associative arrays, since the index is converted into a string and used as a key. E.g. myData[7] is equivalent to myData["7"]. The length property does not return the number of items in the array though. It returns the value of the key with the highest numerical value if parsed as…
Re: Understanding JavaScript Arrays
#9I want to stress that the following will work, but get you in trouble. Javascript does not support associative arrays. var myData = new Array; myData['key1'] = 'value1'; myData['key2'] = 'value2'; myData['key3'] = 'value3'; myData.length; //0
I would say that JS does support associative arrays, since it is the fundamental data type behind objects and arrays. Arrays are basically associative arrays, since the index is converted into a string and used as a key. E.g. myData[7] is equivalent to myData["7"]. The length property does not return the number of items in the array though. It returns the value of the key with the highest numerical value if parsed as…
>>> Moreover building a hash over an Array object is potentially dangerous. If anyone extends Array.prototype with enumerable properties (as, for example, the Prototype.js library does) these will be read in during for…in iterations, wreaking havoc with your logic (or at least requiring you to make use of the clunky hasOwnProperty method). Build hashes over Object and nothing else, since by convention Object.prototype is not augmented.