+ 2

Associatibe Array : JavaScript

Var fruits= []; fruits['name']='apple' ; fruits['color']='red'; here we get fruits.length = 0 as JavaScript doesn't support associative array but HOW JAVASCRIPT CONVERTS or CHANGES fruits array to object and name or color properties are added in Array object while doing the same in console

24th Jan 2017, 6:13 PM
Mahesh Shenoy
Mahesh Shenoy - avatar
1 Answer
+ 2
In fact, in JS, all is object... So an array is an object, and when you access 'fruits' variable by a key, you an implicit declaration of a new property to your array object, if the variable do not contain one already... But this custom property isn't used by the array objet, so the 'length' count only numerical indexed items ^^ If you want only an pseudo associative array ( real built-in don't exist in JS ), you can simply use a basic object declaration, without carrying array behaviour ( and could more safe iterating over object properties ): fruits = { }; ... which is the shortchut declaration of a 'new Object()'.
25th Jan 2017, 8:34 AM
visph
visph - avatar