Array.prototype.findIndex

Array.prototype.findIndex

Array.prototype.findIndex is a method used to find the index of a value within an array.

It accepts a callback as an argument where the callback must return a boolean value, true if the value is the one being searched for, or false if not.

If the value is found, then the method will return the index, otherwise it will return -1 to indicate that the value was not found.

The callback will be provided with each item in the array until the callback returns true.

This method works well for more complex use cases such as searching for objects within an array, but for simpler cases Array.prototype.indexOf might be another good option.

Related topics

© 2024 MWXYZ