Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do find out all elements that match a specific condition in JavaScript?
In JavaScript, there are multiple ways to find elements that match specific conditions. While the underscore.js library provides the _.where() function, modern JavaScript also offers native methods like filter(), every(), and find().
Using _.where() from Underscore.js
The _.where() function belongs to underscore.js, a JavaScript library that provides utility functions. It finds elements that match specified properties in an array of objects.
Syntax
_.where(list, properties)
Parameters:
- list ? The array to search through
- properties ? Object containing the matching criteria
Return value: An array containing all elements that match the specified properties.
Example 1: Finding Users by Property
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
</head>
<body>
<div id="result1"></div>
<script type="text/javascript">
var users = [
{ name: "priya", active: "false" },
{ name: "aishwarya", active: "true" },
{ name: "akanksha", active: "true" },
{ name: "ruby", active: "true" }
];
var activeUsers = _.where(users, { active: "true" });
document.getElementById("result1").innerHTML = JSON.stringify(activeUsers, null, 2);
</script>
</body>
</html>
[
{
"name": "aishwarya",
"active": "true"
},
{
"name": "akanksha",
"active": "true"
},
{
"name": "ruby",
"active": "true"
}
]
Example 2: Finding Cities by State
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
</head>
<body>
<div id="result2"></div>
<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": "Pune", "state": "Maharashtra", "id": "5" }
];
var jharkhandCities = _.where(details, {state: "Jharkhand"});
document.getElementById("result2").innerHTML = JSON.stringify(jharkhandCities, null, 2);
</script>
</body>
</html>
[
{
"city": "Ranchi",
"state": "Jharkhand",
"id": "1"
},
{
"city": "Jamshedpur",
"state": "Jharkhand",
"id": "2"
}
]
Using Native JavaScript Array.filter()
Modern JavaScript provides the filter() method for finding elements that match conditions.
<!DOCTYPE html>
<html>
<body>
<div id="result3"></div>
<script>
let users = [
{ id: 1, name: "priya", age: 25 },
{ id: 2, name: "aishwarya", age: 30 },
{ id: 3, name: "akanksha", age: 22 },
{ id: 4, name: "ruby", age: 35 }
];
// Find users older than 25
let olderUsers = users.filter(user => user.age > 25);
document.getElementById("result3").innerHTML = JSON.stringify(olderUsers, null, 2);
</script>
</body>
</html>
[
{
"id": 2,
"name": "aishwarya",
"age": 30
},
{
"id": 4,
"name": "ruby",
"age": 35
}
]
Using Array.every() for Testing All Elements
The every() method tests whether all elements pass a condition.
<!DOCTYPE html>
<html>
<body>
<div id="result4"></div>
<script>
let numbers = [2, 4, 6, 8, 10];
let allEven = numbers.every(num => num % 2 === 0);
document.getElementById("result4").innerHTML =
"All numbers are even: " + allEven;
</script>
</body>
</html>
All numbers are even: true
Comparison of Methods
| Method | Returns | Use Case | Dependency |
|---|---|---|---|
_.where() |
Array of matching objects | Object property matching | Underscore.js |
Array.filter() |
Array of matching elements | Custom conditions | Native JavaScript |
Array.every() |
Boolean (true/false) | Test if all elements match | Native JavaScript |
Conclusion
While _.where() is useful for simple object property matching, modern JavaScript's filter() method provides more flexibility and doesn't require external libraries. Choose based on your specific needs and project dependencies.
