Different ways to search an item in an array in JavaScript
Searching for an item in a JavaScript array involves determining whether a specific element is present within the array. This process is fundamental for tasks like data validation, filtering, and manipulation, enabling developers to locate and manage elements within arrays in their applications efficiently.
These are the following methods:
Table of Content
- Using find() method
- Using findIndex() method
- Using includes() method
- Using some() method
- Using indexOf() method
- Using filter() method
- Using every() method
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.
const arr = [25, 33, 22, 45, 67, 1, 32, 223];
const greaterElement = arr.find(ele => ele > 50);
console.log(greaterElement);
Output
67
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.
const arr = [25, 33, 22, 45, 67, 1, 32, 223];
let greaterElement = arr.findIndex(ele => ele > 50);
console.log(greaterElement);
Output
4
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.
const arr = ['Beginner','for','Beginner','Javascript','HTML','CSS']
console.log(arr.includes('Beginner'));
console.log(arr.includes('Javascript'));
console.log(arr.includes('CSS'));
console.log(arr.includes('React'));
Output
true true true false
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.
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
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
const arr = ['Beginner','for','Beginner','Javascript','HTML','CSS']
console.log(arr.indexOf('Beginner'));
console.log(arr.indexOf('HTML'));
console.log(arr.indexOf('React'));
Output
0 4 -1
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.
const arr = [25,33,22,45,67,81,32,223];
const greaterElement = arr.filter(ele => ele > 50);
console.log(greaterElement)
Output
[ 67, 81, 223 ]
Using every() method
- In this approach, we will 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);
Example: This example shows the useful of every() method.
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