5 ms·
Understanding JavaScript Arrays
- messel 16y agoforEach,map, and several other ECMA5 methods look like a joy to use
- ccollins 16y agoI 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
- olavk 16y agoI 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 a integer plus one.
- robryan 16y agoAlso you can use associate array notation to access an object property. Found this out after previously using eval to access a dynamic property name.
- code_duck 16y agoyes, that's right - item.akey is interchangeable with item['akey'], but the latter form accepts expressions as the key value.
- deleted 16y ago[deleted]
- teamonkey 16y agoI agree with this quote from the link: >>> 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.
- silentbicycle 16y agoThis is a gotcha in Lua, too, but there it makes a bit more sense - tables ("dicts", if you prefer) explicitly have a key/value part and an array part - if you have a table with keys 1* to N, it's handled internally as an array (with O(1) lookups), but #, the length operator, explicitly gives you the upper bound for the array portion. In practice, this isn't a big deal, since the very different performance etc. means that they're usually used in very different ways. 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. :)
- sesqu 16y agoNo. The length property of an array returns the index of the last value, plus one. Objects are the associative arrays, but they do not have a length. 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.
- megafotze 16y agolol hacker news
- svnv 16y agoIsn'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]); }
- pooj 16y agoClever, but sometimes you don't want to go in reverse.
- jacksoncarter 16y agoOr try this var i = a.length; while (i--){ console.log(a[i]); }
- code_duck 16y agoYes, in a micro-optimized way.
- svnv 16y agoActually, I just realized this does not work, there is an off by 1 error here. jacksoncarter's piece of code works though.
- msie 16y agoThis isn't your father's Array. Sigh.