Getting Array/Index Position for a Selected Value

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://outsideofsociety.idz.net”, “http://www.fontsforflash.com”, “http://www.wildform.com”, “http://www.coolhomepages.com”, “http://www.flasheaven.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:

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:


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:


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.

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:

Thor - thanks a bunch :slight_smile: 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:

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

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

thanks :).

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 :wink: