Home  >  Article  >  Web Front-end  >  How to find all elements matching a specific condition in JavaScript?

How to find all elements matching a specific condition in JavaScript?

王林
王林forward
2023-08-24 09:37:08741browse

JavaScript 中如何找出与特定条件匹配的所有元素?

We can find the specific conditions with the help of the _.ware() function.

_.where() Belongs to underscore.js, a javascript library that provides a variety of functions. _.where() is a function used to find specific elements based on given conditions.

Suppose you want to find all user details of the class, then apply the _.where() function to the list of all sections, passing the condition as the section name. So the names of all users matching the specific criteria will be returned.

grammar

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

This function accepts three parameters as parameters.

  • List - Parameter used to save a list of data.

  • Predicate- This parameter holds the test condition.

  • Context - The text we need to display.

Return value− This function returns all values ​​that meet the conditions.

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

_.where()The function obtains the elements of the array one by one and matches the specified conditions. Long is it true or false. If there is a match, this value is 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 pass a list of elements with multiple attributes to the _.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 pass an array with a number as one of its properties to the _.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

We can use _.findWhere() function instead of _.where() function since both give the 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 example, we will use the _.filter() method to check whether the elements in the array meet the conditions.

<!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 every() method

In this example, we will use the array.every() method to check whether the elements in the array meet the conditions.

<!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 a for loop

Now let’s look at an example using a for loop -

<!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>

The above is the detailed content of How to find all elements matching a specific condition in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete