Isn't this faster for iterating over the elements: var a = ["banana", Math.min, 4, "apple"]; for (var i=a.length; i; i--) { console.log(a[i]); }
Understanding JavaScript Arrays
11–17 of 17 posts
Re: Understanding JavaScript Arrays
#12Isn't this faster for iterating over the elements: var a = ["banana", Math.min, 4, "apple"]; for (var i=a.length; i; i--) { console.log(a[i]); }
var i = a.length;
while (i--){
console.log(a[i]);
}Re: Understanding JavaScript Arrays
#13I 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…
It sounds like Javascript has the same downside, but without any upside.
* Yeah, Lua is 1-indexed. Nothing's perfect. Lisp has all those parens, Python has the whitespace thing...you get over it. :)
Re: Understanding JavaScript Arrays
#14Earlier quoted context omitted.
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…
Also you can use associate array notation to access an object property. Found this out after previously using eval to access a dynamic property name.
Re: Understanding JavaScript Arrays
#15Isn't this faster for iterating over the elements: var a = ["banana", Math.min, 4, "apple"]; for (var i=a.length; i; i--) { console.log(a[i]); }
Re: Understanding JavaScript Arrays
#16I 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…
Arrays are numerically indexed, but if a keystring is a base-10 integer, it will get interpreted as an index. If not, the corresponding property of the array object will be returned instead.
Re: Understanding JavaScript Arrays
#17Isn't this faster for iterating over the elements: var a = ["banana", Math.min, 4, "apple"]; for (var i=a.length; i; i--) { console.log(a[i]); }