How do find out all elements that match a specific condition in JavaScript?


We can find the specific condition with the help of the _.whare() function.

The _.where() belongs to underscore.js a javascript library that provides versatile functions. The _.where() is a function used to find the specific element according to the given conditions.

Suppose you want to find all the user details of the class and then apply the _.where() function to the list of all the sections and pass the condition as the section name. So the name of all the users with the specific condition will be returned.

Syntax

_.where( list, [predicate], [context] )

The function accepts three-argument as a parameter.

  • List − the parameter used to hold the list of data.

  • Predicate − This parameter holds the test condition.

  • Context − the text we need to display.

Return value − the function returns all value with the matched condition.

Example 1: Passing an array to the _.where() function

The _.where() function takes the array’s elements one by one and matches the specified condition. Long is true or not. If it’ll match, then the value will be returned.

<!DOCTYPE html>
<html>
<head>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
</head>
<body>
   <script type="text/javascript">
      var users = [
         { name: "priya", Long: "false" },
         { name: "aishwarya", Long: "true" },
         { name: "akanksha", Long: "true" },
         { name: "ruby", Long: "true" },
      ];
      document.write(JSON.stringify(_.where(users, { Long: "true" }))); 
   </script>
</body>
</html>

Example 2

In the following example we are passing a list of elements with a number of properties to _.where() function −

<!DOCTYPE html>
<html>
<head>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
</head>
<body>
   <script type="text/javascript">
      var details = [
         { "city": "Ranchi", "state": "Jharkhand", "id": "1" },
         { "city": "Jamshedpur", "state": "Jharkhand", "id": "2" },
         { "city": "Hyderabad", "state": "Telangana", "id": "3" },
         { "city": "Delhi", "state": "Delhi", "id": "4" },
         { "city": "Delhi", "Pune": "Maharastra", "id": "5" }
      ];
      document.write(JSON.stringify(_.where(details,{state:"Jharkhand"})));
   </script>
</body>
</html>

Example: 3

In this example we are passing an array having numbers as one of its properties to _.where() function −

<!DOCTYPE html>
<html>
<head>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
</head>
<body>
   <script type="text/javascript">
      var users = [
         { id: 1, name: "priya" },
         { id: 2, name: "aishwarya" },
         { id: 3, name: "akanksha" },
         { id: 4, name: "ruby" },
         { id: 5, name: "Aman" },
      ];
      document.write(JSON.stringify(_.where(users, { id: 5 })));
   </script>
</body>
</html>

Example 4: _.where() function as _.findWhere() function

Instead of _.where() function we can used _.findWhere() function because both gives same output.

<!DOCTYPE html>
<html>
<head>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
</head>
<body>
   <script type="text/javascript">
      var users = [
         { id: 1, name: "priya" },
         { id: 2, name: "aishwarya" },
         { id: 3, name: "akanksha" },
         { id: 4, name: "ruby" },
         { id: 5, name: "Aman" },
      ];
      document.write(JSON.stringify(_.findWhere(users, { id: 5 })));
   </script>
</body>
</html>

Example 5: Using the _.filter() method

In this case, we will use the _.filter() method to check whether the elements in an array have passed the condition or not.

<!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>_.filter() method example</title>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
   <div id="filter"></div>
</head>
<body>
   <script>
      let arr = [2, 4, 6, 8, 10];
      let flag = false;
      let getEvenNum = _.filter(arr, function (num) {
         return num % 2 == 0;
      });
      if (JSON.stringify(getEvenNum) === JSON.stringify(arr)) {
         flag = true;
      }
      document.getElementById("filter").innerHTML ="The array elements passed the condition : " + flag;
   </script>
</body>
</html>

Example 6: Using the every() method

In this case, we will use the array.every() method to check whether the elements in an array have passed the condition or not.

<!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>Array.every() method example</title>
   <div id="every"></div>
</head>
<body>
   <script>
      let arr = new Array(2, 4, 6, 8, 10);
      let getEvenNum = arr.every(function (num) {
         return num % 2 == 0;
      });
      document.getElementById("every").innerHTML ="The array elements have passed the condition : " + getEvenNum;
   </script>
</body>
</html>

Example 7: Using the for loop

Let us now see an example using for loops −

<!DOCTYPE html>
<html lang="en">
<head>
   <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: 06-Dec-2022

885 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements