[{'break_time':'09:00'},{' break_time ':'09:00'},{' break_time ':'09:00'},{' break_time ':'10:00'},{' break_time ':'10:00'},{' break_time ':'15:00'},{' break_time ':'09:00'}] how do I get the number of break_time and number of 09:00,10:00 and 15:00 in javascript? Keep getting TypeError: ret3.forEach/filter is not a function
Asked
Active
Viewed 54 times
-5
-
Possible duplicate of [Counting the occurrences / frequency of array elements](https://stackoverflow.com/questions/5667888/counting-the-occurrences-frequency-of-array-elements) – salvatore Feb 02 '19 at 07:09
2 Answers
0
You can create a object and increase the count for each matches and get a final object with key
as a time value and value
as a count of that time:
var arr = [{
'break_time': '09:00'
}, {
'break_time': '09:00'
}, {
'break_time': '09:00'
}, {
'break_time': '10:00'
}, {
'break_time': '10:00'
}, {
'break_time': '15:00'
}, {
'break_time': '09:00'
}];
var obj = {};
arr.forEach((arrObj) => {
if(obj[arrObj.break_time]){
obj[arrObj.break_time]++;
} else {
obj[arrObj.break_time] = 1;
}
});
console.log(obj);

Ankit Agarwal
- 30,378
- 5
- 37
- 62
0
Using a quasi-functional approach, you can write
var arr = [{'break_time':'09:00'},{'break_time':'09:00'},{'break_time':'09:00'},{'break_time':'10:00'},{'break_time':'10:00'},{'break_time':'15:00'},{'break_time':'09:00'}]
const breakCount = arr.filter(function(it) { return it.break_time }).length
const countByValue = arr.reduce(function(acc, curr) {
acc[curr.break_time] = (acc[curr.break_time] || 0) + 1
return acc
}, {})
console.log(breakCount)
console.log(countByValue)
EDITED
On ol versions of NodeJs or browsers you can rearrange Ankit Agarwal's answer as follows:
var arr = [{
'break_time': '09:00'
}, {
'break_time': '09:00'
}, {
'break_time': '09:00'
}, {
'break_time': '10:00'
}, {
'break_time': '10:00'
}, {
'break_time': '15:00'
}, {
'break_time': '09:00'
}];
var obj = {};
for(var i = 0; i < arr.length; i++) {
var arrObj = arr[i];
if(obj[arrObj.break_time]){
obj[arrObj.break_time]++;
} else {
obj[arrObj.break_time] = 1;
}
}
console.log(obj);

Pietro Martinelli
- 1,776
- 14
- 16
-
So the thing is this when I try to implement any of this, I get the error ret3.filter TypeError: ret3.filter is not a function or ret3.forEach is not a function .. I am getting the data from mysql. – DennisKip Feb 02 '19 at 07:43
-
I can also get the in this format [["09:00"],["09:00"],["09:00"],["10:00"],["10:00"],["15:00"],["09:00"]] ..I am not sure what the format is exactly. kindly advice. – DennisKip Feb 02 '19 at 08:09
-
Ok, you are in the context of a NodeJs application, isn't it? In old versions of NodeJs `filter` and `forEach` methods were not available, so... you can rearrange Ankit Agarwal's answer as follows: ``` var arr = [...]; var obj = {}; for(var i = 0, i < arr.length; i++) { var arrObj = arr[i]; if(obj[arrObj.break_time]){ obj[arrObj.break_time]++; } else { obj[arrObj.break_time] = 1; } } console.log(obj); ``` – Pietro Martinelli Feb 02 '19 at 10:08
-
I updated my first answer including this old-style-javascript flavour... – Pietro Martinelli Feb 02 '19 at 10:14