# Getting Array/Index Position for a Selected Value

**URL:** <https://forum.kirupa.com/t/getting-array-index-position-for-a-selected-value/21921>\
**Category:** flash\
**Created:** [May 28, 2003, 12:22am UTC](https://forum.kirupa.com/t/getting-array-index-position-for-a-selected-value/21921 "2003-05-28T00:22:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [May 28, 2003, 12:22am UTC](https://forum.kirupa.com/t/getting-array-index-position-for-a-selected-value/21921/1 "2003-05-28T00:22:52Z")

</div>

Hey everyone,  
I have a combobox that displays a list of items from an array (we’ll call this Array1). Of course, an item can be selected, and the user is displayed a small output describing the item that was selected.

I created another array that contains the URL for items in the above array, Array1. How can I get the index value of a selected item from Array1? Here is the code I used: [AS]resources = new Array(“Flash Developer . NL”, “OutsideOfSociety”, “Fonts for Flash”, “Wildform”, “CoolHomePages”, “FlashHeaven”, “Flash4All”);  
links = new Array(“[http://www.flashdeveloper.nl](http://www.flashdeveloper.nl)”, “[http://outsideofsociety.idz.net](http://outsideofsociety.idz.net)”, “[http://www.fontsforflash.com](http://www.fontsforflash.com)”, “[http://www.wildform.com](http://www.wildform.com)”, “[http://www.coolhomepages.com](http://www.coolhomepages.com)”, “[http://www.flasheaven.de](http://www.flasheaven.de)”, “[http://www.flash4all.de](http://www.flash4all.de)”);  
for (i=0; i\<resources.length; i++) {  
flash.addItem(resources\*);  
}  
function listDisplay(component) {  
list = component.getSelectedItem().label;  
trace(list);  
// location = component.getSelectedItem().number;  
// trace(location);  
}  
flash.setChangeHandler(“listDisplay”);[/AS]

For example, if I selecte FontsForFlash, Flash outputs Fonts for Flash in the Output Window. How can I display the array position of Fonts for Flash instead of the name Fonts for Flash?

Thanks!  
Kirupa :ub:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 1:07am UTC](https://forum.kirupa.com/t/getting-array-index-position-for-a-selected-value/21921/2 "2003-05-28T01:07:09Z")

</div>

kirupa asking for actionscript help :!:. haha, j/k. anyway, after flipping through my o’reilly, i couldn’t find an Array prototype that gets the index from the array, so you know what this means…yep, we’re gonna have to make one of our own. put this code pretty much anywhere but preferably in the first frame of the movie:

```auto

Array.prototype.getIndex = function(data) {
		for (i=0; i<this.length; ++i) {
			if (this* == data) {
				return i;
			}
		}
		return -1;
	};

```

this function returns a number. now how this works…in your code, once you copy this code in, you can add this line to get the index of a certain item:

```auto

location = component.getIndex("Fonts for Flash");

```

this should return 2 since it was the third item, but with indexes, counting starts with 0 (so…0, 1, 2 for the third item). hope this helps :).  
oh and btw, if the prototype can’t find what it’s looking for, it will return -1, so be sure to check for that in your code.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 1:08am UTC](https://forum.kirupa.com/t/getting-array-index-position-for-a-selected-value/21921/3 "2003-05-28T01:08:24Z")

</div>

Ai, the solution was fairly simple. I simply told Flash to search the original array for the item found in list and get the index value that way. Basically, I placed this chunk of code:[AS]for (k=0; k\<resources.length; k++) {  
if (list == resources[k]) {  
location = links[k];  
break;  
}  
}  
getURL(location, “\_blank”);[/AS]

…right below trace(list);

Cheers!  
Kirupa :beam:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 1:09am UTC](https://forum.kirupa.com/t/getting-array-index-position-for-a-selected-value/21921/4 "2003-05-28T01:09:38Z")

</div>

Thor - thanks a bunch 🙂 Unfortunately I found a more primitive method that does almost the same thing. I will take note of your method for use in the future though.

Cheers!  
Kirupa :asian:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 1:10am UTC](https://forum.kirupa.com/t/getting-array-index-position-for-a-selected-value/21921/5 "2003-05-28T01:10:57Z")

</div>

…yeah you could do that too. i just went over mountains and oceans so that you can use that function wherever, thus making it universal. but if it’s just for this one time, your code should suffice.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 1:36am UTC](https://forum.kirupa.com/t/getting-array-index-position-for-a-selected-value/21921/6 "2003-05-28T01:36:59Z")

</div>

Then again, this would make an excellent AS trick written in your name though - so all isn’t lost 🙂 I’ll add your code up shortly (with you getting credit of course).

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 1:45am UTC](https://forum.kirupa.com/t/getting-array-index-position-for-a-selected-value/21921/7 "2003-05-28T01:45:33Z")

</div>

thanks :).

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 2:18am UTC](https://forum.kirupa.com/t/getting-array-index-position-for-a-selected-value/21921/8 "2003-05-28T02:18:52Z")

</div>

In case anyone is wondering what I’m using this for, it will replace the resources drop-down menu displayed at the footer. This is to make creating new forum color schemes easier. The animation is attached to this post.

Cheers!  
Kirupa 😉
