

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find out all elements that matches a specific condition in JavaScript?
Javascript has many frameworks in which underscore.js is one of them. It has provided many functions in which _.where() is a function used to find elements based on a specific condition.
This method will display the elements based on whether they passed the condition or not. For suppose if we passed a condition that how many of the people from the provided array have salaries equal to 15000, then the method _.where() scrutinizes every element whether it passed the condition or not. If any of the elements passed the condition, then that particular element will be displayed as the output.
syntax
_.where( list, testCondition);
It accepts an array to scrutinize and a scrutinizing condition to evaluate the elements. The elements that have passed the condition will be displayed as the output.
Example-1
In the following example, a condition is passed regarding ages and output is displayed based on the condition.
<html> <body> <script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/0.10.0/lodash.min.js"></script> </head> <body> <script> var people = [ {"name": "Dhoni", "age": 38}, {"name": "kohli", "age": 22}, {"name": "Rohit", "age": 28}, {"name": "dhawan", "age": 28} ] document.write(JSON.stringify(_.where(people, {age: 28}))); </script> </body> </html>
Output
[{"name":"akansha","age":28},{"name":"preeti","age":28}]
Example-2
In the following example, the array "students" is scrutinized by passing a condition regarding their id's and results are displayed as shown in the output.
<html> <body> <script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/0.10.0/lodash.min.js"></script> </head> <body> <script> var students = [ {"name": "Ravi", "id": 45}, {"name": "Surya", "id": 45}, {"name": "Chandra", "id": 47}, {"name": "guru", "id": 40} ] document.write(JSON.stringify(_.where(students, {id:45}))); </script> </body> </html>
Output
[{"name":"Ravi","id":45},{"name":"Surya","id":45}]
- Related Questions & Answers
- How to find the column index in an R data frame that matches a condition?
- Find document that matches same array elements in MongoDB?
- Find array elements that are out of order in JavaScript
- Finding matches in two elements JavaScript
- How to find all tables that contains two specific columns in MySQL?
- How can I find all matches to a regular expression in Python?
- JavaScript array: Find all elements that appear more than n times
- C++ program to find out the number of pairs in an array that satisfy a given condition
- Find all strings that match specific pattern in a dictionary in C++
- With JavaScript RegExp how to search a string for text that matches regexp?
- Program to find out the XOR values of specific elements from a generated list in Python
- Find all pairs that sum to a target value in JavaScript
- Find records in MongoDB that does NOT match a condition?
- Find all close matches of input string from a list in Python
- How to pull all elements from an array in MongoDB without any condition?