How to check whether provided elements in an array have passed a specified condition or not in JavaScript?


In this article, we are going to discuss whether provided elements in an array have passed a specified condition or not in JavaScript. Here, we can use the _.filter() method, every() method, some() method, filter() method, and for loop. They return true if every element in an array has met the condition, otherwise, they return false.

Using the Array.filter() method

This Array.filter() method is a JavaScript function. Array.filter() method will be creating a new array containing the elements that are passed in the condition which is defined by function in program.

This Array.filter() method will not execute if elements are empty or NULL. Array.filter() does not modify the existing array.

Syntax

array.filter(function(currentValue, index, arr), thisValue)

Example 1

In the example below, we have used Array.filter() method to filter the elements which are satisfying the condition defined in the function.

This method will create a new array and display the output. The original array will not be disturbed or modified.

<!DOCTYPE html> <html> <body> <h1>The cars which are below five lakhs are:</h1> <h2 id="demo"></h2> <script> var Cost_of_cars = [200000, 560000, 450000, 800000, 500000]; var filter_cars = Cost_of_cars.filter(func); document.getElementById("demo").innerHTML = filter_cars; function func(value, index, array) { return value <= 500000; } </script> </body> </html>

Using the _.filter() method

This is also a method to check whether given elements in an array have passed a specified condition or not. _.filter() method

Example

Consider this example, we have used _.filter() method to separate the elements which are obeying to the condition mentioned in the function.

This method will return the output by creating a new array. Thus the main array will not be changed.

<html> <head> <script type="text/javascript" src= "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> </script> </head> <body> <script> var array = [11,20,33,44,50,65,75,66,99,100,42,54] var even_num = _.filter(array, function(value){ return value % 2 == 0; }); document.write("The values which are EVEN in the array are: "); document.write(even_num); </script> </body> </html>

Using the Array.every() method

This Array.every() is a method in JavaScript. This method verifies whether every element in the array has passed the condition which is given through a function.

This method will return output in a Boolean value. If the all the given elements passed the condition, it will return the output as true and if it doesn’t qualifies the condition it returns false.

Example

The following example below, is using Array.every() method. This will return the output in Boolean values. In this below document, some elements aren’t satisfying the condition mentioned in the function. Thus it returned false.

<!DOCTYPE html> <html> <body> <title> Array.every() Method</title> <h2 id="demo"></h2> <script> const salary = [30000, 40000, 50000, 60000, 70000, 80000]; document.getElementById("demo").innerHTML = salary.every(func); function func(emp) { return emp >= 40000; } </script> </body> </html>

Using Array.some() method

This Array.some() is a method in JavaScript. This method will verify if any array element has passed the condition. This method returns the output as true (and stops) if any one of the element in array has passed the condition. And return the output as false when all the elements in the array has not passed the condition. This method will not execute if the element in the array is empty It does not modify the existing array.

Example

In this example below, we used Array.some() method to check the elements have passed the condition or not which is declared in function. This method will return true in the below situation because Array.some() will return true if one element in the array have satisfied the condition.

<!DOCTYPE html> <html> <body> <title>Array.some()</title> <h3 id="demo"></h3> <script> const Weights = [56, 61, 65, 70, 71, 80, 99, 106]; document.getElementById("demo").innerHTML = Weights.some(know_weight); function know_weight(weight) { return weight > 60; } </script> </body> </html>

Using the for loop

We can also achieve this task using a simple for loop following is an example for that −

Example

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Using for loop example</title> <div id="every"></div> </head> <body> <script> let arr = new Array(2, 4, 6, 8, 10); let flag = true; for (let i = 0; i < arr.length; i++) { if (arr[i] > 10) { flag = false; break; } } document.getElementById("every").innerHTML = "The array elements have passed the condition : " + flag; </script> </body> </html>

Updated on: 19-Sep-2022

102 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements