javascript index of array code example
Example 1: js get index of item in array
array.indexOf("item");
Example 2: js array get index
var fruits = ["Banana", "Orange", "Apple", "Mango"];
return fruits.indexOf("Apple");
Example 3: javasript array indexof
var array = [2, 9, 9];
array.indexOf(2);
array.indexOf(7);
array.indexOf(9, 2);
array.indexOf(2, -1);
array.indexOf(2, -3);
Example 4: get index of element in array javascript
var scores = [10, 20, 30, 10, 40, 20];
console.log(scores.indexOf(30));
console.log(scores.indexOf(50));
Example 5: how to get the index of an array in javascript
search = (arr, item) => { return arr.indexOf(item); }
Example 6: make indexOF in js
function indexOf(arr, value) {
for (let [i, e] of arr.entries()) {
if (value == e) return i;
}
return -1;
}
indexOf([1,2,3], 2);