5 methods to Check value exist in JavaScript Array

In this post, we are going to explore different 5 methods to check if a value exists in JavaScript Array or not. Here, we are going to use built-in array methods to find the value in the JavaScript object array or array.

1. Using array.indexof() methods


The array. indexof() method is used to find the position of an array element, it returns the index of the first occurrence of matched element in array else returns -1 if the element is not found.

var letters = ['A', 36, 'B', 'Rat', 50, 67];




console.log('index of element exist in array  :',letters.indexOf(36));
console.log('index of element not exist in array  :',letters.indexOf(47));


Output

index of element exist in array  : 1
index of element not exist in array  : -1

Check insensitive

Sometimes, we have value in the array are lower and upper case. So to check values that exist in the array we have to convert all elements of an array into the lower case before we use indexof() function.



var letters = ['A', 'BAT', 'b', 'Rat'];


const caseinsentive = letters.map(value => value.toLowerCase());

// ['a', 'bat', 'b', 'rat'];

console.log('index of element exist in array  :',caseinsentive.indexOf('rat'));

Output

index of element exist in array  : 3

2.Using array.includes() methods


array.includes() methods returns TRUE if value exist in array else returns false.We can use this method with primitives type values as we used undefined values to check if it exists in an array.

var letters = ['A', 36, 'B', 'Rat', 50, 67,undefined,null];




console.log('Value exists in array:',letters.includes(36));
console.log('Value exists in array:',letters.includes(undefined));

Output

Value exists in array: true
Value exists in array: true

Check insensitive

Sometimes we have values in the array that are lower and upper case. So to check values that exist in the array we have to convert all elements of an array into the lower case before we make use of includes() function.


var letters = ['A', 'BAT', 'b', 'Rat'];


const caseinsentive = letters.map(value => value.toLowerCase());

// ['a', 'bat', 'b', 'rat'];

console.log('index of element exist in array  :',caseinsentive.includes('rat'));

Output

index of element exist in array  : true

3.Using array.Find() method


The array.Find() method returns the first array element that passes the given test function. if none of the array element passes the given test function then it returns undefined

const array = [15, 20, 8, 130, 44];

const ele_found = array1.find(element => element%5==0);

console.log('array elellmet divible by five:',ele_found);

Output

array elellmet divible by five : 15

Find element in Array of objects

const subjects = [
  {subject :"Math",marks:90},
  {subject:"Physics",marks:99},
  {subject :"Chem",marks:100}
 
];



const sub_name = subjects.find(s => s.marks>90);



console.log('subject marks greater than 90:',sub_name);

Output

subject marks greater than 90: { subject: 'Physics', marks: 99 }

4. Using array.filter()


array. filter() is a standard built-in function of the array, it returns a new array with all elements which passes the condition implemented by a callback function. The array. filter() iterate over the array and pass each array element to the callback function if the callback function returns true the array element includes it in the return result array.

let Employee = [
    {Empname: 'John', salary: 40000},
    {Empname: 'jack', salary: 50000},
    {Empname: 'Racx', salary: 60000},
    {Empname: 'Max', salary: 70000},
    {Empname: 'Robin', salary: 80000}
];
 
let higherSalaryEmp = Employee.filter(function (e) {
 return e.salary > 40000;
})
console.log(higherSalaryEmp)

Output

[{
  Dep: "IT",
  Empname: "jack",
  salary: 50000
}, {
  Dep: "IT",
  Empname: "Racx",
  salary: 60000
}, {
  Dep: "IT",
  Empname: "Max",
  salary: 70000
}, {
  Dep: "IT",
  Empname: "Robin",
  salary: 80000
}]

5. Using array.some() with array of object


The array. some() check for at least one element that passes the test condition .The test condition is implement by using callback function.

const alp = [{ letter: 'A' ,value:65}, { letter: 'c',value:67 },{ letter: 'B',value:66 }];




console.log('Value exist in array:',alp.some(alp => alp.letter === 'A'));

Output

Value exist in array: true

Check Case Insensitive

Sometimes we have values in the array that are lower and upper case. So to check values that exist in the array we have to convert all elements of an array into the lower case before calling some() function.


const alp = [{ letter: 'A' ,value:65}, { letter: 'c',value:67 },{ letter: 'B',value:66 }];

 


console.log('Value exist in array:',alp.some(alp => alp.letter.toLowerCase() === 'c'));

Output

Value exist in array: true

Summary:

We have explored 5 different methods to check if the value exists in JavaScript Array by using the array built-in methods.