Recipe 6.3 Searching for Matching Elements in an Array

6.3.1 Problem

You want to find the first element in an array that matches a specified value.

6.3.2 Solution

Use a for statement to loop through an array, and use a break statement once a match has been found.

6.3.3 Discussion

When you are searching for the first element in an array that matches a specified value, you should use a for statement, as shown in Recipe 6.2, but with the addition of a break statement to exit the loop once the match has been found.

A break statement used within a for statement causes the loop to exit once it is encountered. Note that the break statement is generally placed within an if statement, so it is reached only when a certain condition is met. The importance of the break statement when searching for the first matching element is two-fold. First of all, you should not needlessly loop through the remaining elements of an array once the match has been found, since it would waste processing time. In the following example, the break statement exits the loop after the second iteration, saving six more needless iterations. Imagine the savings if there were a thousand more elements. Furthermore, the break statement is vital when searching for the first match because it ensures that only the first element is matched and that subsequent matches are ignored. If the break statement is omitted in the following example, all matching elements are displayed, as opposed to the first one only.

// Create an array with eight elements. myArray = ["a", "b", "c", "d", "a", "b", "c", "d"]; // Specify what we want to search for. searchString = "b"; // Use a for statement to loop through, potentially, all the elements of the array. for (var i = 0; i < myArray.length; i++) {   // Check whether the current element matches the search value.   if (myArray[i] == searchString) {     // Do something with the matching element, if necessary. In this example, display     // a message in the Output window for testing purposes.     trace("Element with index " + i + " found to match " + searchString);     // Include a break statement to exit the for loop once a match has been found.     break;   }   }

You can also search for the last matching element of an array by reversing the order in which the for statement loops through the array. Initialize the index variable to Array.length - 1 and loop until it reaches 0 by decrementing the index variable, as follows.

myArray = ["a", "b", "c", "d", "a", "b", "c", "d"]; searchString = "b"; // Loop backward through the array. In this example, we'll find the "b" at index 5. for (var i = myArray.length - 1; i >= 0; i--) {   if (myArray[i] == searchString) {     trace("Element with index " + i + " found to match " + searchString);     break;   }   }

6.3.4 See Also

Recipe 6.10



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net