JavaScript array indexof() with example

JavaScript

In this post JavaScript array indexof() with example, JavaScript array.indexof() is an inbuilt method that is used to find the index of the array element. Also, we will learn about How to make array.indexof() case-insensitive.

JS array.indexof() method


The array. find() method searches the given element in the array and returns its index. The search is performed from the start to end elements of the array. if a given element is not found it returns -1.

If the elements occur multiple times, array.index() method will return the index of the first occurrence of the element.

Syntax

array.indexof(element,FromIndex)

Parameters

  • element: The index of the array element we are looking in the array. It is required parameter.
  • FromIndex: It is an optional parameter and it is starts index from where the index() method will start the search.
    • if we omit the fromindex parameter then the search starts from the first element of the array.
    • The fromindex can be negative or positive.

Important Note:

  • The array.indexOf() methods compare search element to elements of the Array using a strict equality algorithm that is similar to the triple-equals operator(=== ).
  • The array indexof() method executes a case-insensitive search.

Return Value

It returns the index of the array element otherwise returns -1.

JavaScript array.indexof() examples


1. Array.indexof() to find all occurrence of item

In this example, we are searching the index of the number 10 in the whole array. If position is found then we are pushing it to a new array and return all indexes of number 10 in the array.

Example

//Js code to  Find all occurrence of a number in the array

function findindex(int,myarray)
{
  var array =[],count =-1;
  while((count=myarray.indexOf(int,count+1)) >= 0) 
  array.push(count);
  return array;
}

let myarray = [10, 20, 30, 10, 20, 10];
console.log('All indexes of number 10 :',findindex(10,myarray));

Output

All indexes of number 10 : [ 1, 3 ]

2. Using fromindex of array.indexof()

In this example, we are passing the second parameter to the array.indexof() method to finding the index of the element from index -1,-5.

Example

let myarray = [10, 20, 30, 40, 20, 10];

console.log('Index :',myarray.indexOf(10,-1));
console.log('index :',myarray.indexOf(40,-5));

The Output will be as follows:

Index : 5
index : 3

3. array.indexof() case-insensitive

To find indexes of all elements of the array regardless of case. First, we are converting all elements of the array to lowercase using the toLowerCase() and then finding the index of the element.

To make array.indexof() method case-insensitive we can use toLowerCase() and toUpperCase() methods.

Using toLowerCase() Method

var myarray = ['This', 'Is', 'dEV', 'eNUM']
    indexOf = (myarray, val) => myarray.findIndex(elem => val.toLowerCase() === elem.toLowerCase());

console.log('Uisng toLowerCase():')
console.log( indexOf(myarray, 'dev')   ) 
console.log(  indexOf(myarray, 'enum')  ) 
console.log(  indexOf(myarray, 'is')  ) 



Output

2
3
1

Using toUpperCase() Method

To find indexes of all elements of the array regardless of case. First, we are converting all elements of the array to uppercase using the toUpperCase() method and finding the index of the element.

var myarray = ['This', 'Is', 'dEV', 'eNUM']
    indexOf = (myarray, val) => myarray.findIndex(elem => val.toUpperCase() === elem.toUpperCase());

console.log('Using toUpperCase() method:\n ')

console.log(indexOf(myarray, 'DEV')   ) 
console.log(  indexOf(myarray, 'ENUM')  ) 
console.log(  indexOf(myarray, 'IS')  ) 

The Output will be:

Using toUpperCase() method:
 
2
3
1

4.Array.indexof() with array of objects

The JavaScript array. indexof() method can be used to get the index of the array of objects. In the below example, we will learn how to get the index of an array of objects. We are using the JavaScript map() method.

let stuarray = [{
  name: 'john',
  sub: 'Math',
  mark : 100
},
{
  name: 'rack',
  sub: 'Phy',
  mark : 100
}];

index = stuarray.map(function(e) { return e.name; }).indexOf('rack');

console.log('Index of array object :',index);

Output will be

Index of array object : 1

Summary:

In this post, we have Explained the JS array indexof() method with the help of examples. We also explained, How to do a case-insensitive index search using a JavaScript array.indexof() method.JavaScript array.find() is an inbuilt method that used to find the index of an array element.