Array indexOf()
JavaScript Array indexOf() Definition
Searches within a series. The criterion for this search is the value we enter. If it finds the element you searched for in the array, it prints the number of rows. If the search we are looking for is not in the array returns -1.
If anything is unclear (start, end), it scans the array from the beginning.
array.indexOf(item, start)
item ( Required ) : The item to search for .
start ( Optional ) :Enter where to start the search.
Let’s understand the logic with a small example:
let names = ["abby", "brisa", "bob", "alice"];
console.log(names.indexOf("abby"));
console.log(names.indexOf("brisa"));
console.log(names.indexOf("bob"));
console.log(names.indexOf("alice"));
0
1
2
3
We can make an example with the initial value:
let names = ["brisa", "brisa", "bob", "alice", "brisa"];
console.log(names.indexOf("brisa",2));
4
Searches for after the initial value entered. This percentage does not see elements 0. and 1. in the above example.
Without indexOf ()
If we didn’t have the indexOf () method, we would do the following.
let cars = ["bmw", "audi", "toyota", "mercedes"];
for (var i = 0; i < cars.length && cars[i] != "toyota"; i++) { }
console.log("toyota:" + i);
toyota: 2
But since we have a method called indexOf (), we don’t need to do this.
let cars = ["bmw", "audi", "toyota", "mercedes"];
var index = cars.indexOf("toyota");
console.log("toyota:" + index)
toyota: 2
JavaScript Array includes() Examples
Example 1
let arr = ["zero", "first", "second", "third"];
console.log(arr.indexOf("zero"));
console.log(arr.indexOf("first"));
console.log(arr.indexOf("second"));
console.log(arr.indexOf("third"));
console.log(arr.indexOf("sixth"));
console.log(arr.indexOf("seventh"));
output:
0
1
2
3
-1
-1
Example 2
let user1 = {
name: "abby",
surname: "allen"
}
let user2 = {
name: "brisa",
surname: "quenn"
}
var users = [user1, user2];
let arr1 = users.indexOf(user1);
let arr2 = users.indexOf(user2);
console.log(arr1);
console.log(arr2);
output:
0
1
if we want to reach the content of the elements we do:
console.log(users[arr1]);
console.log(users[arr2]);
output:
{name: “abby”, surname: “allen”}
{name: “brisa”, surname: “quenn”}
Example 3
let arr = [];
let row = ["zero", "first", "second", "third"];
let sec = "second";
let index = row.indexOf(sec);
while (index != -1) {
arr.push(index);
index = row.indexOf(sec, index + 1);
}
console.log(arr)
output:
2
Polyfill
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {
var k;
// 1. Let o be the result of calling ToObject passing
// the this value as the argument.
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
// 2. Let lenValue be the result of calling the Get
// internal method of o with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = o.length >>> 0;
// 4. If len is 0, return -1.
if (len === 0) {
return -1;
}
// 5. If argument fromIndex was passed let n be
// ToInteger(fromIndex); else let n be 0.
var n = fromIndex | 0;
// 6. If n >= len, return -1.
if (n >= len) {
return -1;
}
// 7. If n >= 0, then Let k be n.
// 8. Else, n<0, Let k be len - abs(n).
// If k is less than 0, then let k be 0.
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
// 9. Repeat, while k < len
while (k < len) {
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the
// HasProperty internal method of o with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
// i. Let elementK be the result of calling the Get
// internal method of o with the argument ToString(k).
// ii. Let same be the result of applying the
// Strict Equality Comparison Algorithm to
// searchElement and elementK.
// iii. If same is true, return k.
if (k in o && o[k] === searchElement) {
return k;
}
k++;
}
return -1;
};
}
Browser Support
Chrome | yes |
Edge | 1.5 |
Firefox | 1.5 |
Internet Explorer | 9 |
Opera | yes |
Safari | yes |
Android webview | yes |
Chrome for Android | yes |
Edge mobile | yes |
Firefox for Android | 4 |
Opera Android | yes |
3 Comments »