
The code example also defines a search predicate method named EndsWithSaurus, which accepts a string parameter and returns a Boolean value indicating whether the input string ends in "saurus". An array of strings is created, containing 8 dinosaur names, two of which (at positions 1 and 5) end with "saurus". The following code example demonstrates all three overloads of the FindIndex generic method.

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the Array that starts at the specified index and contains the specified number of elements. Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the Array that extends from the specified index to the last element. Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire Array. Naturally, if you remove or add entries (or change their prop2 values), you need to update your mapping object as well.Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the first occurrence within an Array or a portion of it. Then if you need to find the entry with prop2 = "yutu", you can do this: var entry = prop2map

(Or, again, you could use a for loop or any of your other options.) Your various iteration options are covered in this other answer.īut if you're always going to be using the same property for this lookup, and if the property values are unique, you can loop just once and create an object to map them: var prop2map = The function I've given it saves the index of the matching entry, then returns true to stop the iteration.

That uses the new(ish) Array#some function, which loops through the entries in the array until the function you give it returns true. However, on a system with ES5 features (or if you install a shim), that iteration can be done fairly concisely: var index If the condition changes a lot, then you'll have to loop through and look at the objects therein to see if they match the condition.

You cannot, something has to iterate through the array (at least once). How can I get the index of the object tha match a condition (without iterate along the array)?
