Skip to content


Searching Array Indexes with indexOf and lastIndexOf methods

package
{
	import flash.display.Sprite;

	public class ArrayFind extends Sprite
	{
		private var myArray:Array = new Array("one", "two", "three", "five", "ten", "twenty", "two", "sixty");

		public function ArrayFind()
		{
			// find the location of "five"
			trace ( myArray.indexOf( "five" ) ); // outputs 3

			// find the location of "two" from the beginning
			trace ( myArray.indexOf( "two" ) ); // outputs 1

			// find the location of "two" from the end
			trace ( myArray.lastIndexOf( "two" ) ); // outputs 6

			// check to see if element is in the array
			trace ( myArray.indexOf( "five hundred" ) ); // outputs -1
		}
	}
}

Posted in Actionscript 3, Arrays.


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Agustin says

    that is something useful I’ve never used…



Some HTML is OK

or, reply to this post via trackback.