# Info about associative arrays

**URL:** https://forum.servoy.com/t/info-about-associative-arrays/15911
**Category:** Classic Servoy
**Created:** [May 7, 2012, 2:51pm UTC](https://forum.servoy.com/t/info-about-associative-arrays/15911 "2012-05-07T14:51:04Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![pentamsi](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@pentamsi](https://forum.servoy.com/u/pentamsi)
#### Post date: [May 7, 2012, 2:51pm UTC](https://forum.servoy.com/t/info-about-associative-arrays/15911/1 "2012-05-07T14:51:04Z")

</div>

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](https://www.servoy.com/forum/viewtopic.php?f=12&t=11425&p=57099&hilit=associative+arrays#p57099) but i don’t know how to travel all the array of add some items with specific key. Thanks

---

<div class="post-metadata">

### Author: ![Joas](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/joas/32/5095_2.png) [@Joas](https://forum.servoy.com/u/Joas)
#### Post date: [May 8, 2012, 7:18am UTC](https://forum.servoy.com/t/info-about-associative-arrays/15911/2 "2012-05-08T07:18:12Z")

</div>

In an associative array you can loop using the “in” keyword:

```auto
	for (var i in array) {
		application.output("key: " + i + ", value: " + array[i]);
	}

```

Adding new values with a specific key goes like this:

```auto
	array.specific_key1 = "value1";

	array["specific_key2"] = "value2";

	var key = "specific_key3";
	array[key] = "value3";

```

---

<div class="post-metadata">

### Author: ![pentamsi](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@pentamsi](https://forum.servoy.com/u/pentamsi)
#### Post date: [May 8, 2012, 9:54am UTC](https://forum.servoy.com/t/info-about-associative-arrays/15911/3 "2012-05-08T09:54:58Z")

</div>

Okey, thanks for all Joas! this is so usefull!

---

<div class="post-metadata">

### Author: ![pbakker](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@pbakker](https://forum.servoy.com/u/pbakker)
#### Post date: [May 8, 2012, 10:53am UTC](https://forum.servoy.com/t/info-about-associative-arrays/15911/4 "2012-05-08T10:53:36Z")

</div>

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

```auto
var x = [] //short for new Array()
x.key1 = 'value1'
x['key2'] = 'value2'

```

You can also do:

```auto
var x = {} //short for new Object()
x.key1 = 'value1'
x['key2'] = 'value2'

```

Or even ```  
var x = {key1: ‘value1’,‘key2’:‘value2’}

```auto

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.
```

---

<div class="post-metadata">

### Author: ![pentamsi](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@pentamsi](https://forum.servoy.com/u/pentamsi)
#### Post date: [May 8, 2012, 2:45pm UTC](https://forum.servoy.com/t/info-about-associative-arrays/15911/5 "2012-05-08T14:45:56Z")

</div>

Thanks for the info pbakker i use and object instead of an Array.

---

<div class="post-metadata">

### Author: ![djlapin](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@djlapin](https://forum.servoy.com/u/djlapin)
#### Post date: [May 11, 2012, 7:09am UTC](https://forum.servoy.com/t/info-about-associative-arrays/15911/6 "2012-05-11T07:09:33Z")

</div>

Paul,

Then this array would _not_ have a length of 2?

Thank you,  
Don

---

<div class="post-metadata">

### Author: ![pbakker](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@pbakker](https://forum.servoy.com/u/pbakker)
#### Post date: [May 11, 2012, 9:07am UTC](https://forum.servoy.com/t/info-about-associative-arrays/15911/7 "2012-05-11T09:07:26Z")

</div>

Correct
