Check if value exists in array JavaScript ES6

While writing code it is often seen that when we have to find out whether a value array may exist or not. In Javascript, there are several ways to check whether a particular value exists in the array.

In this tutorial, I show you all the possible ways to check if a value exists in the array of objects.

Check if value exists in array JavaScript ES6

1. Check Value exists in array using Indexof() method –

We can use indexof() method of javascript to check whether a particular value or item exists in the array. The indexof() method returns the index of element if it is found in array otherwise it returns -1 if it’s is not found.

The best thing about the indexof() method is it works on all the browsers.

Example 1:

Let see the below code example using indexof() method to check if the value exists in the array.

<script> 
var empName = ["Bruce","Tom","Mike","Barb"];
// Check if a value exists in the empName array
if(empName.indexOf("Mike") != -1){ // it return 2 because Mike exist at index 2
  console.log("Employee found!");
} else{
  console.log("Employee not found!")
}
</script>

Note: The indexof() method is case sensitive means capital “M” in Mike treats small “m” differently. So it is advised to always use the toLowerCase() method while comparing.

2. Check Value Exist in Array using Includes() method

Now in modern programming, ES6 provides includes() method that is used to check the value exist in the array. It returns true if the value exists in the array else returns false.

Example 2:

<script> 
var empName = ["Bruce","Tom","Mike","Barb"];
console.log(empName.includes("Mike")); //true
console.log(empName.includes("John")); //false
</script>

By default, the includes() method searches from all elements of an array but optionally you can pass the second parameter as a starting index to start the search for different positions.

Example 3:

<script> 
var empName = ["Bruce","Tom","Mike","Barb"]; 
console.log(empName.includes("Mike", 3)); //false
</script>

In the above example, I started the search from the 3rd Index of an empName array and it returns false because “Mike” exists at Index 2.

Note: if the starting index is less than Zero, all elements of an array will be searched.

inlcudes() method is case sensitive preferably used for modern application and it only works with modern browsers if you plan to test your code in IE as well go for indexof() method.

3. Check value exists in an array using JavaScript Array filter

There is one more alternative way to check whether a particular value exists in an array of objects using the JavaScript array filter. it returns another array of objects if the value exists and you can use the length() method to check the value exists or not. See below code example –

Example 4:

<script> 
const items =[
{ name : "PS4", price: 200},
{ name : "iPhone" , price: 450},
{ name : "iPod" , price : 80},
{ name : "Watch" , price : 150},
{ name : "MacBook", price : 900 },
{ name : "TV" , price : 600},
{ name : "Keyboard", price : 50 },
{ name : "iPad" , price : 300}
]

const filterItems = items.filter((item) => {
return item.name == "Watch";
})
if(filterItems > 0){
console.log("Item exist in array");
}
else{
console.log("Item not exist in array");
}
</script>

Here I have used the javascript arrow function same can be achieved without the arrow function as well. The filter method is popular when we have a large array of objects and we need to fetch the matching element too.

4. find method JavaScript

The find() method is used to find an element in the array if the element exists in an array then it returns the element and if not exist it return nothing. Below is the working example of the find() method –

Suppose you have below array –

const arritems =[
{ name : "PS4", price: 200},
{ name : "iPhone" , price: 450},
{ name : "iPod" , price : 80},
{ name : "Watch" , price : 150},
{ name : "MacBook", price : 900 },
{ name : "TV" , price : 600},
{ name : "Keyboard", price : 50 },
{ name : "iPad" , price : 300}
]
const findItem =  arritems.find((item) => {
  return item.name == "Watch";
})
console.log(findItem);

Check if value exists in array JavaScript using find method
Here in this example I have checked “Watch” exists in an array and it returns the element in finditem array.

Note – find() method is not supported in I.E it works only in modern browsers.

5. Some method in JavaScript

The some() method returns true if the item is found otherwise false. One point you have to remember about some() method it returns true if the condition is satisfied for any element in the array and it ignores the rest of the elements in the array.

const arritems =[
{ name : "PS4", price: 200},
{ name : "iPhone" , price: 450},
{ name : "iPod" , price : 80},
{ name : "Watch" , price : 150},
{ name : "MacBook", price : 900 },
{ name : "TV" , price : 600},
{ name : "Keyboard", price : 50 },
{ name : "iPad" , price : 300}
]
const checkinexpensiveItems =  arritems.some((item) => {
 return item.price <=  100;
})
console.log(checkinexpensiveItems);

some method in JavaScript

Here in the above example, I have checked if the array contains any item whose price is less than equal to $ 100 so it returns true because the array does have items having a price less than 100$.

6. Every method in JavaScript

The every() method is almost the same as some() method the only difference is it checks each and every element of the array and returns true if all the elements of an array satisfy the condition if any of the elements in the array didn’t satisfy the condition it returns false.

Let’s understand the every() method with the below example –

const arritems =[
{ name : "PS4", price: 200},
{ name : "iPhone" , price: 450},
{ name : "iPod" , price : 80},
{ name : "Watch" , price : 150},
{ name : "MacBook", price : 900 },
{ name : "TV" , price : 600},
{ name : "Keyboard", price : 50 },
{ name : "iPad" , price : 300}
]
const checkeveryItem =  arritems.every((item) => {
return item.price <  100;
})
console.log(checkeveryItem);

every method in JavaScript

I am using the same example as I used in some() methods here I have checked that any item price is less than 100 $ but we have items that have a price more than 100 $ as well so it returns false.

So these are some methods that you can use to check if the value exists in array JavaScript. There are few other methods such as a map which can also be used to find the items in the array.

Conclusion

This is how you can check if the value exists in array JavaScript. I show you the inbuilt method as well as the alternative approach which can be used to check that any particular item exists in the array.

Hope you understand the modern es6 JavaScript methods which you can use to check if the value exists in the array.

 

Leave a Reply

Your email address will not be published. Required fields are marked *