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
Count number of entries in an object having specific values in multiple keys JavaScript
In JavaScript, you often need to count objects in an array that match specific criteria across multiple properties. This is commonly done using the filter() method combined with conditional logic.
Suppose we have an array of objects representing trade data:
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 need to count objects where the "from" property equals "USA" and the "to" property equals "INDIA".
Using filter() and length
The most straightforward approach is to filter matching objects and return the array length:
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 countMatches = (arr = [], from = 'USA', to = 'INDIA') => {
return arr.filter(obj => obj.from === from && obj.to === to).length;
};
console.log(countMatches(arr));
2
Using reduce() for Better Performance
For large datasets, reduce() is more efficient as it doesn't create an intermediate array:
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 countWithReduce = (arr = [], from = 'USA', to = 'INDIA') => {
return arr.reduce((count, obj) => {
return (obj.from === from && obj.to === to) ? count + 1 : count;
}, 0);
};
console.log(countWithReduce(arr));
2
Generic Multi-Criteria Counter
For more flexibility, create a function that accepts multiple criteria as an object:
const arr = [
{"goods":"Wheat", "from":"GHANA", "to":"AUSTRALIA"},
{"goods":"Wheat", "from":"USA", "to":"INDIA"},
{"goods":"Rice", "from":"USA", "to":"INDIA"},
{"goods":"Wheat", "from":"USA", "to":"INDIA"},
];
const countByCriteria = (arr, criteria) => {
return arr.filter(obj => {
return Object.keys(criteria).every(key => obj[key] === criteria[key]);
}).length;
};
// Count objects with multiple criteria
console.log(countByCriteria(arr, {from: "USA", to: "INDIA"}));
console.log(countByCriteria(arr, {from: "USA", to: "INDIA", goods: "Wheat"}));
3 2
Comparison of Methods
| Method | Performance | Readability | Memory Usage |
|---|---|---|---|
filter().length |
Good | Excellent | Higher (creates array) |
reduce() |
Better | Good | Lower (no intermediate array) |
| Generic function | Good | Excellent | Higher |
Conclusion
Use filter().length for simple cases and readability. For large datasets or performance-critical code, reduce() is more efficient as it avoids creating intermediate arrays.
