JavaScript Find Position of Element in Array

To find index or position of an element in an array JavaScript; Through this tutorial, i am going to show you how to find the position or index of element in an array using JavaScript array indexOf() and lastIndexOf() methods.

How to get the index of an item or element in a JavaScript array

There are two methods available in javaScript to get or find the first and last index or position of element in array; as shown below:

  • JavaScript ArrayindexOf() –  To find first index (position) of a specified value or elements.
  • JavaScript ArraylastIndexOf() – To find last index (position) of a specified value or elements.

JavaScript array indexOf()

Using javaScript indexOf(), you can find the index or position of an element in an array. This method returns the index or position of the first occurrence the element that you want to find, or -1 if the element is not found.

Syntax of the  javaScriptindexOf() method

Array.indexOf(search, startIndex)

Note that:- The indexOf() method accepts two parameters.

  1. The search argument is the element that you want to find in the array.
  2. The startIndex is an array index at which the function starts the search.

Note:- The startIndex parameter value can be a positive or negative integer.

Example 1 – To find index of an element in an array in javascript using indexOf() 

Let, you have a numeric array named arr. See the following:

var arr = [10, 20, 30, 40, 50, 70, 10];

And you want to find index of an element in an array in javascript; So see the following example for that:

var arr = [10, 20, 30, 40, 50, 70, 10, 50];
console.log(arr.indexOf(10)); // 0
console.log(arr.indexOf(50)); // 4
console.log(arr.indexOf(70)); // 5
console.log(arr.indexOf(30)); // 2

Example 2 – To find index of an element in an array object in javascript

Let, you have the following array of objects, where each object has different properties. See the following:

var obj = [
    {name: 'John Doe', age: 30},
    {name: 'Tom Bush', age: 20},
    {name: 'Bill Gate', age: 25}
];

The following statement returns -1 even though the first element of the obj array and the searchElement have the same values in the name and ages properties. This is because they are two different objects.

console.log(obj.indexOf({
    name: 'John Doe',
    age: 30
})); // -1

JavaScript array lastIndexOf()

This is similar to the indexOf() method, but one difference in these. The Javascript lastIndexOf() method is used to find the last occurrence position of an array. It returns -1 if it cannot find the element.

Syntax of the lastIndexOf() method:

Array.lastIndexOf(searchElement[, fromIndex = Array.length - 1])

Note:- The basic different from the indexOf() method and the lastIndexOf() method. One is backword search method and another is forward search method.

Example 1 – To find last index of array in javascript

Let, you have an array like following, and want to find last index of array in JavaScript using lastIndexOf() method. So, See the following example:

var arr = [10, 20, 30, 40, 50, 70, 10, 40];
console.log(arr.lastIndexOf(10));// 6
console.log(arr.lastIndexOf(40));// 7

If finding elements in the array, and it is not available in it, returns -1. See the following:

var arr = [10, 20, 30, 40, 50, 70, 10, 40];
console.log(arr.lastIndexOf(100));// -1

Conclusion

In this tutorial, you have learned how to use the JavaScript array indexOf() and lastIndexOf() methods to find the elements in an array.

More JavaScript Tutorials

Recommended:-JavaScript Arrays

Leave a Comment