- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count number of entries in an object having specific values in multiple keys JavaScript
Suppose, we have an array of objects like this −
const arr = [ {"goods":"Wheat ", "from":"GHANA", "to":"AUSTRALIA"}, {"goods":"Wheat", "from":"USA", "to":"INDIA"}, {"goods":"Wheat", "from":"SINGAPORE", "to":"MALAYSIA"}, {"goods":"Wheat", "from":"USA", "to":"INDIA"}, ];
We are required to write a JavaScript function that takes in one such array. The goal of function is to return an array of all such objects from the original array that have value "USA" for the "from" property of objects and value "INDIA" for the "to" property of objects.
Example
const arr = [ {"goods":"Wheat ", "from":"GHANA", "to":"AUSTRALIA"}, {"goods":"Wheat", "from":"USA", "to":"INDIA"}, {"goods":"Wheat", "from":"SINGAPORE", "to":"MALAYSIA"}, {"goods":"Wheat", "from":"USA", "to":"INDIA"}, ]; const findDesiredLength = (arr = [], from = 'USA', to = 'INDIA') => { const filtered = arr.filter(el => { if(el.from === from && el.to === to){ return true; } }); const { length: l } = filtered || []; return l; }; console.log(findDesiredLength(arr));
Output
And the output in the console will be −
2
- Related Articles
- Compare keys & values in a JSON object when one object has extra keys in JavaScript
- Comparing objects in JavaScript and return array of common keys having common values
- How to count the number of columns having specific value in MySQL?
- Python dictionary with keys having multiple inputs
- Get multiple count in a single MySQL query for specific column values
- Iterate through Object keys and manipulate the key values in JavaScript
- JavaScript: replacing object keys with an array
- JavaScript Count the number of unique elements in an array of objects by an object property?
- Changing value of nested object keys in JavaScript
- Search a value in an object with number keys with MongoDB
- Get only specific values in an array of objects in JavaScript?
- How to set dynamic property keys to an object in JavaScript?
- MySQL query to select the values having multiple occurrence and display their count
- Split array entries to form object in JavaScript
- Fetching object keys using recursion in JavaScript

Advertisements