<ontopic :) > On Mon, Mar 19, 2012 at 3:03 PM, Dmitriy Sintsov questpc@rambler.ru wrote:
On 19.03.2012 17:23, Krinkle wrote:
On Mon, Mar 19, 2012 at 9:35 AM, Daniel Friesen lists@nadir-seen-fire.com**wrote:
On Mon, 19 Mar 2012 00:40:54 -0700, Dmitriy Sintsovquestpc@rambler.ru
wrote: var jqgmap = [];
for ( var mapIndex in jqgmap ) {
This is VERY bad JavaScript coding practice. Please use $.each().
This is rather exaggerated. Even more when looking at that suggestion.
Arrays should, indeed, not be enumerated with a for-in loop. Arrays in JS can only contain numeral indices, so they should simply be iterated with a simple for-loop like this `for (i = 0; i< myArr.length; i += 1) { .. }` (maybe cache length for slight performance gain by reducing property lookups).
My array is numeric but sparse,
myArr = []; myArr[0] = a; myArr[1] = b; So I cannot just use incremental key iteration, at least existence of element should be checked.
If you use an array, use `myArr[myArr.length] = value;` or `myArr.push(value);` to add something to it. And `.slice(..)` or `.splice(..)` to remove something from it. Never set a key directly or remove a key directly.
If you want a non-linear array, create an object instead. JavaScript allows to do this (because Array is just an extension of Object in javascript), but that doesn't mean you should.
If you need non-linear keys, don't create an array!
<code> var myObj = {}; // not [] myObj.property = value;
var customProp = getPropName(); myObj[customProp] = value;
for ( var key in myObj ) { // myObj[key] } </code>
-- Krinkle