Array.Find < Array

The Find function accepts a function that will be used to determine the criteria of finding an object. The function will be called for each element in the array passing in the current item as the first parameter. The function must return true or false and the first element to return true will be returned and looping through the elements will be halted. Below is an example:

var a = someFunctionThatReturnsAnArrayOfObjects();
var myObject = a.Find(function(i){
    return i.SomeProperty == "someValue";
});

 

If there is an object in the array that has a “SomeProperty” value of “someValue”, it will be returned and set to myObject. If no match is found, myObject will be null.

Array.FindAll

FindAll is similar in that it takes a function for comparison. The only difference is that for each element that returns true for the function, it will be added to the return array. If no match is found, the array will be empty.

var b = someOtherFunctionThatReturnsAnArray();
var myObjects = b.FindAll(function(i){
    return i.Property > 4;
});

 

myObjects will be an array and contain every element that has a Property value greater than 4.

Last edited May 29, 2011 at 8:57 PM by dahrkdaiz, version 3

Comments

No comments yet.