Return to Snippet

Revision: 24982
at March 17, 2010 10:00 by adrianparr


Initial Code
function findIndexInArray(value:Object, arr:Array):Number {
	for (var i:uint=0; i < arr.length; i++) {
		if (arr[i]==value) {
			return i;
		}
	}
	return NaN;
}

var myArray:Array = new Array("a","b","c","d","e","f","g");
trace(findIndexInArray("c", myArray));

// OUTPUT
// 2

Initial URL

                                

Initial Description
If the item cannot be found then the return value is NaN. This function only returns the index of the first occurence found, so it's not very good if you have multiple occurrences of the same value in the array.

Initial Title
AS3 Search for a Value in Array and Return it's Position Index

Initial Tags
number, search, array

Initial Language
ActionScript 3