pussyger.blogg.se

Findindex javascript
Findindex javascript





Naturally, if you remove or add entries (or change their prop2 values), you need to update your mapping object as well. 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 = If no element of the array satisfies the. The function I've given it saves the index of the matching entry, then returns true to stop the iteration. The findIndex() method returns the index of the first element in array that satisfies the given testing function. Narrator The array methods find and findIndex are used when you only need to return a single value within an array.

findindex javascript

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 3includes truefalse let resulr arr.includes (6) console.log (resulr) //true 1. 2lastIndexOf -1 let index2 arr.lastIndexOf (6) console.log (index2) //5 1. 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. 1indexOf let index1 arr.indexOf (6) console.log (index1) //2 1. You cannot, something has to iterate through the array (at least once).

findindex javascript

How can I get the index of the object tha match a condition (without iterate along the array)?







Findindex javascript