DEV Community

Cover image for Five Array methods to check or find item(s) in Array.
Khan M. Tabish
Khan M. Tabish

Posted on

Five Array methods to check or find item(s) in Array.

There are five different ways to check if an item or object exist within an array. Based on our use cases, we can use any of the array method to achieve the same.

Here in this blog I will tell you the uses cases of all five array methods and also when and where it should be used.

Array.includes

Includes is checking that if the given item is present in the array or not.

if item exist then it return true otherwise false.'

We can't use the comparison operator in the includes like greater than or less than. It is only used to test the direct values. Here is an example.

var numbers = [1, 2, 3, 4, 5];
var isNumberExist = numbers.includes(1);
console.log(isNumberExist); //true

var isNumberExist = numbers.includes(7);
console.log(isNumberExist); //false
Enter fullscreen mode Exit fullscreen mode

In the above example, I am checking that if the 1 is present in the numbers array or not. in that case its returning true. in the same way, when I am checking for the 7, its returning false.

Array.some

This Array method return a callback.

This method is used to check if any of the array item is satisfying the condition, then it will return the true otherwise it will return the false.

Let me explain the same with an example.

var numbers = [1, 2, 3, 4, 5];
var isNumberExist = numbers.some((item) => item <= 7);
console.log(isNumberExist); //true

var isNumberExist = numbers.some((item) => item >= 7);
console.log(isNumberExist); //false

Enter fullscreen mode Exit fullscreen mode

n the above example, you can see that the first statement, var isNumberExist = numbers.some((item) => item <= 7); is checking that if any of the item is less than or equal to 7 is present in the array. In that case its returning true.

and the second statement, var isNumberExist = numbers.some((item) => item >= 7); is checking that if any of the array item is greater than or equal to 7 is present in the array or not. in that case its returning false.

Array.every

his Array method is also returning a callback.

Array.every method is just the opposite of the Array.some. It is checking that, if all the items of the array is satisfying the given condition.

Explaining this with the help of an example.

var numbers = [1, 2, 3, 4, 5];
var isNumberExist = numbers.every((item) => item <= 7);
console.log(isNumberExist); //true

var isNumberExist = numbers.every((item) => item >= 3);
console.log(isNumberExist); //false

Enter fullscreen mode Exit fullscreen mode

In this example, the first statement numbers.every((item) => item <= 7); is checking that all the items of the array is less than 7. So in this case it is true. All the items of the array is less than or equal to 7.

The second statement numbers.every((item) => item >= 3); is checking that if items of the array is greater than or equal to 3. so in that case only 3, 4 and 5 are greater than or equal to 3. SO it will return false.

Array.filter

This Array method will also return a callback.

Array.filter is used to filter the array on the basis of a condition. if condition satisfied then it will return an array with the matching items.

Let's understand with the code.

var numbers = [1, 2, 3, 4, 5];
var isNumberExist = numbers.filter((item) => item < 7);
console.log(isNumberExist); //[1, 2, 3, 4, 5]

var isNumberExist = numbers.filter((item) => item > 3);
console.log(isNumberExist); //[4, 5]

Enter fullscreen mode Exit fullscreen mode

In the above example, the first filter statement numbers.filter((item) => item < 7);is checking that if array has numbers which are less than 7. so in that case all the items of the array is less than 7. so it will return an array [1, 2, 3, 4, 5].

The second statement numbers.filter((item) => item > 3); is checking that, if items of the array is less than 3, if satisfied then it will return that matched items in the form of an array. so in this case it will return the [4, 5] because only 4 and 5 are greater than 3.

Array.find

The objective of this array method is to find an item within the array, but there is a catch in that. It will not return the all the matched item and it will also not return true or false.

It will return the first matched item only.

For example:

var numbers = [1, 2, 3, 4, 5];
var isNumberExist = numbers.find((item) => item < 7);
console.log(isNumberExist); //1

var isNumberExist = numbers.find((item) => item > 3);
console.log(isNumberExist); //4

Enter fullscreen mode Exit fullscreen mode

In the above case, the first statement numbers.find((item) => item < 7); will return the 1 because the first matched item is 1. the second statement numbers.find((item) => item > 3); will return the 4, because the first matched item is 4 from the array which is greater than 3.

Top comments (0)