Yeah i know array is what you have been living with because you think you do not require object at some places. And talking about array, you gonna choose associative arrays for sure, right eh ?
Well on the sad note you do not get associative arrays inside javascript. Its not that you do not get, but they are kinda pain, buggy and somehow its not supported. You will get lotsa junk values when you create one. So does that mean there is nothing much to play around with.
Hang on guys !! Here is an alternate i use in my javascript code, hope you will like it. Its JSON object. Here you can see the code. It is self explanatory, a simple for loop parses the associative like array 🙂
arr1={keys:["name","city","state","country"]}; arr2={values:["realin","Noida","delhi","india"]}; for(i=0;i<arr1.keys.length;i++) { alert(arr1.keys[i]+"=>"+arr2.values[i]); } |
Hope you like this little workaround. Do let me know if you have any amendments, feedback, critics 🙂
Cheers !!
Realin !
Although we require same number of keys and values for an associative array. Its quite obvious that the index of an array would be equal to the number of elements. But still there is a nice update by a random user below.
Updated code by feelinglucky
var arr1 = {keys:["name","city","state","country"]}; var arr2 = {values:["realin","Noida","delhi","india"]}; for(var i in arr1.keys) { if (arr1.keys[i]&& arr2.values[i]) { console.info(arr1.keys[i] + "=>" + arr2.values[i]); } } |