I want to know if are by default some methods to get the index and the key value of an associative array, and if are some basic functions to manipulate this kind of arrays. Thanks for all. I see this article but i don’t know how to travel all the array of add some items with specific key. Thanks
In an associative array you can loop using the “in” keyword:
for (var i in array) {
application.output("key: " + i + ", value: " + array[i]);
}
Adding new values with a specific key goes like this:
array.specific_key1 = "value1";
array["specific_key2"] = "value2";
var key = "specific_key3";
array[key] = "value3";
Okey, thanks for all Joas! this is so usefull!
Hi,
There is no such thing as an associative Array in JavaScript. Every type in JavaScript is also an Object and Objects are standard maps of key/value pairs.
So when you’re instantiating an Array and then setting key/value pair properties on it, you’re not adding items to the Array, but you are adding properties to the Array, which is possible because an Array is also an Object.
So, instead of doing
var x = [] //short for new Array()
x.key1 = 'value1'
x['key2'] = 'value2'
You can also do:
var x = {} //short for new Object()
x.key1 = 'value1'
x['key2'] = 'value2'
Or even ```
var x = {key1: ‘value1’,‘key2’:‘value2’}
Best to not abuse an Array as an object, because you might think that the length property of the array increases when adding properties, but it doesn't. All the API Methods and Properties specific to the Array have no knowledge of the properties you added to it.
Thanks for the info pbakker i use and object instead of an Array.
Paul,
Then this array would not have a length of 2?
Thank you,
Don
Correct