I have referred these questions listed below, but didn't helped me out.
I want to get count from object with specific value. For example, I have this array list,
var testData = [
{
"issue_type": "Warning",
"created_date": "2019-05-13T13:43:16.437Z",
},
{
"issue_type": "Warning",
"created_date": "2019-05-13T13:45:16.330Z",
},
{
"issue_type": "Alert",
"created_date": "2019-05-13T13:43:16.437Z",
},
{
"issue_type": "Alert",
"created_date": "2019-05-13T13:45:16.330Z",
}
]
I want to count how many objects with key "issue_type"="Warning"
are there.
I tried with this loop but it is not what I'm looking for,
var counts = {};
for (var i = 0; i < arr.length; i++) {
var num = arr[i];
counts[num] = counts[num] ? counts[num] + 1 : 1;
}
console.log(count['issue_type']);
Please suggest me the way out.
You could take the value as key for counting.
var testData = [{ issue_type: "Warning", created_date: "2019-05-13T13:43:16.437Z" }, { issue_type: "Warning", created_date: "2019-05-13T13:45:16.330Z" }, { issue_type: "Alert", created_date: "2019-05-13T13:43:16.437Z" }, { issue_type: "Alert", created_date: "2019-05-13T13:45:16.330Z" }],
counts = testData.reduce((c, { issue_type: key }) => (c[key] = (c[key] || 0) + 1, c), {});
console.log(counts);