6 ways to find elements in Array in JavaScript
By: Amitav Mishra
Tags: Arrays, Javascript Tips, Javascript Development, Web Development, JavaScript

Posted by jscurious.com

There are various methods available to find elements or their positions in Array. But we can always choose the method that satisfies our search criteria i.e. whether we are searching for element or its position in the array.

We will be discussing about 6 ways to find in array. The various methods are find , findIndex , includes , indexOf , lastIndexOf and loops ( for , filter , map ). Let's check them out in brief.

The find() method

The Array find() method returns the first matched element in array that satisfies a condition. The find() method takes a function as argument which returns true or false based on some condition. The find() method executes this function for each element of array. If the function returns true for any element then that element will be returned by the find() method and it stops checking further for rest of the elements. If no element passed the condition then find() method will return undefined .

let score = [55, 73, 82, 66, 48]; let value = score.find(val => val > 60);

// returns the first element that passed the condition console.log(value); // 73

The findIndex() method

The Array findIndex() method returns the index of first matched element in array that satisfies a condition. This method also takes a function argument which returns true or false.

The findIndex() method works the same way as find() method except find() returns the first matched element and findIndex() returns the index of the first matched element. If no match found in the array then findIndex() returns -1.

let score = [55, 73, 82, 66, 48]; let index = score.findIndex(val => val > 60);

// returns the index of first element that passed the condition console.log(index); // 1

The includes() method

This method checks if a specified element is present or not in an array and returns a boolean value. If the specified element is present in array then it returns true else false. This method is case sensitive.

let teams = ['CSK', 'KKR', 'MI', 'RCB']; console.log(teams.includes('KKR')); // true console.log(teams.includes('rcb')); // false

This method accepts an optional parameter which specifies the position to start the search for element. If we specify a position, then it will start looking for element from that position till end of the array. The default value is 0.

let teams = ['CSK', 'KKR', 'MI', 'RCB']; console.log(teams.includes('KKR', 2)); // false

The indexOf() method

This method returns the index of first occurrence of matched element in array. If no element is matched then it returns -1. This method is case sensitive.

let shop = ['Amazon', 'Flipkart', 'Wallmart', 'Myntra', 'Flipkart']; console.log(shop.indexOf('Myntra')); // 3 console.log(shop.indexOf('Flipkart')); // 1 console.log(shop.indexOf('amazon')); // -1

This method accepts an optional parameter which specifies the position to start the search for element. If we specify a position, then it will start looking for element from that position till end of the array. The default value is 0.

let shop = ['Amazon', 'Flipkart', 'Wallmart', 'Myntra']; console.log(shop.indexOf('Amazon', 1)); // -1

If you want to check if any element is present or not in array, then you can just simply check the index of that element in array. So if index >= 0 , then that element exists, else if index = -1 , then element doesn't exist in array.

The lastIndexOf method

This method returns the index of last occurrence of matched element in array. This method searches element from end to beginning of array. If no element is matched then it returns -1. This method is case sensitive.

let items = ['car', 'phone', 'watch', 'car', 'bike']; console.log(items.lastIndexOf('car')); // 3 console.log(items.lastIndexOf('watch')); // 2 console.log(items.lastIndexOf('BIKE')); // -1

This method accepts an optional parameter which specifies the position to start the search for element. If we specify a position, then it will start looking for element from that position till beginning of the array.

let items = ['car', 'phone', 'watch', 'car', 'bike'];

//start searching from index 2 to 0 console.log(items.lastIndexOf('car', 2)); // 0

Looping through Array elements and find

We can loop through each array elements and find any elements or their positions based on any condition.

Using for loop

let arr = ['Gold', 'Silver', 'Platinum', 'Iron']; let isPresent = false; for(let item of arr) {

if(item === 'Silver') isPresent = true;

} if(isPresent) console.log('Silver Exists !');

else console.log('Silver Not found !');

Using map() method

Let’s see one example to find numbers which are greater than 30.

let marks = [53, 29, 65, 22, 71]; let pass = []; marks.map(val => {

if(val >= 30) pass.push(val);

}); console.log(pass); // [53, 65, 71]

Using filter() method

This method finds and filters out elements that do not pass the condition. This method takes a function as argument which returns true or false. This function executes for each element in array. If the function returns true for any element then only that element gets included in the returned array.

let marks = [53, 29, 65, 22, 71]; let pass = marks.filter(val => val >= 30); console.log(pass); // [53, 65, 71]

References