How to find the index of an element in an array? The example calls the following three overloads of the IndexOf method to find the index of a string in a string array:. If the searched array does not contain the item, JavaScript indexOf function delivers the return value of -1. indexOf method finds the index of the first matching element in the array and returns it. Learn how to check if elements or characters are present in Arrays or Strings with the indexOf method in javascript. If an element is not present in an array, it returns -1. let array = [ { prop1: 'value1' , }, { prop2: 'value2' , }, { prop3: 'value3' , }]; function arrayMap() { let pos = array .map ( function (e) { return e.prop2; }).indexOf ( 'value2' ); console.log ( "Index of 'value2' is = " + pos); } arrayMap (); Javascript find index of an array that contains object. indexOf() when there is still no native support. searchElement 1. // Whether it is actually faster remains to be seen. Content is available under these licenses. IndexOf(Array, Object, Int32), to determine the first occurrence of the string "the" in the fourth to the last elements of a string array. Note: The first item has position 0, the second item has position 1, and so on. array.index . Its output is as follows − index is : 2 Code:
Let’s Connect