In this article, we will see the different ways to search for an item in an array item in Javascript. When working with arrays, there are many situations where we need to find a specific element of an array.
Javascript provides us with some methods to find the occurrences of an item in an array, some of them are explained below:
- Using find() method
- Using findIndex() method
- Using includes() method
- Using some() method
- Using indexOf() method
- Using filter() method
- Using every() method
Approach 1: Using find() method:
The Javascript arr.find() method is used to get the value of the first element in the array that satisfies the provided condition. It checks all the elements of an array and whichever the first element satisfies the condition is going to print. This function will not work having the empty array elements and also does not change the original array.
Syntax:
array.find(function(currentValue, index, arr), thisValue);
Example: In this example, we will use the Javascript Array find() method to search for an element in an array. This method searches for the first element that is greater than 50 and returns it.
Javascript
const arr = [25, 33, 22, 45, 67, 1, 32, 223]; const greaterElement = arr.find(ele => ele > 50); console.log(greaterElement); |
67
Approach 2: Using findIndex() method:
The Javascript Array.findIndex() method is used to return the first index of the element in a given array that satisfies the provided testing function (passed in by the user while calling). Otherwise, if no data is found then the value of -1 is returned.
Syntax:
array.findIndex(function(currentValue, index, arr), thisValue)
Example: This example returns the index of the first element that is greater than 50.
Javascript
const arr = [25, 33, 22, 45, 67, 1, 32, 223]; let greaterElement = arr.findIndex(ele => ele > 50); console.log(greaterElement); |
4
Approach 3: Using includes() method:
The Javascript array.includes() method is used to know whether a particular element is present in the array or not and accordingly, it returns true or false i.e., if the element is present, then it returns true otherwise false.
Syntax:
array.includes(searchElement, start)
Example: This example shows the use of the array includes() method in Javascript.
Javascript
const arr = [ 'Geeks' , 'for' , 'neveropen' , 'Javascript' , 'HTML' , 'CSS' ] console.log(arr.includes( 'neveropen' )); console.log(arr.includes( 'Javascript' )); console.log(arr.includes( 'CSS' )); console.log(arr.includes( 'React' )); |
Output:
true true true false
Approach 4: Using some() method
The Javascript arr.some() method checks whether at least one of the elements of the array satisfies the condition checked by the argument method.
Syntax:
arr.some(callback(element,index,array),thisArg)
Example: In this example, we will check for some elements in the array that are greater than 50 or not. The function returns true as there are elements greater than 50 in the array.
Javascript
const arr = [25, 33, 22, 45, 67, 1, 32, 223]; console.log(arr.some((element) => element > 50)); console.log(arr.some((element) => element < 40)); console.log(arr.some((element) => element > 70)); |
Output:
true true true
Approach 5: Using indexOf() method
The JavaScript Array indexOf() Method is used to find the index of the first occurrence of the search element provided as the argument to the method.
Syntax:
array.indexOf(element, start)
Example: This example shows the use of the array indexOf() method in Javascript
Javascript
const arr = [ 'Geeks' , 'for' , 'neveropen' , 'Javascript' , 'HTML' , 'CSS' ] console.log(arr.indexOf( 'Geeks' )); console.log(arr.indexOf( 'HTML' )); console.log(arr.indexOf( 'React' )); |
Output:
0 4 -1
Approach 6: Using filter() method
The JavaScript Array filter() Method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument method.
Syntax:
array.filter(callback(element, index, arr), thisValue)
Example: This example checks for elements greater than 50 in an array using the Array filter() method.
Javascript
const arr = [25,33,22,45,67,81,32,223]; const greaterElement = arr.filter(ele => ele > 50); console.log(greaterElement) |
Output:
[ 67, 81, 223 ]
Approach 7: Using every() method
Follow the steps to perform a check on the element.
- Initialize the test list.
- Use every method which checks the opposite of the condition we want to check if all element satisfies the condition every return True else return False.
- Print result.
Syntax:
arr.every(callback(element,index,array),thisArg)
Javascript
const arr = [25, 33, 22, 45, 67, 1, 32, 223]; console.log(!arr.every((element) => element < 50)); console.log(!arr.every((element) => element > 40)); console.log(!arr.every((element) => element < 70)); |
Output:
true true true