Hello I have a list of dates, stored in an array; there are multiple entries for the same date. I want to get the occurrences of each date in the for of json object, something like :
Expected output:
dates:[{date:11/10/2019,count: 5},{date: 11/11/2019,count:4},{date:11/12/2019, count: 5 }]
My array data looks like:
[
"11/10/2019, 10:44:16 PM",
"11/10/2019, 10:48:26 AM",
"11/10/2019, 10:59:41 PM",
"11/10/2019, 11:37:53 AM",
"11/10/2019, 12:19:52 PM",
"11/11/2019, 12:46:59 PM",
"11/11/2019, 12:59:08 PM",
"11/11/2019, 1:32:23 PM",
"11/11/2019, 3:08:01 AM",
"11/12/2019, 3:21:39 AM",
"11/12/2019, 3:26:00 PM",
"11/12/2019, 3:27:13 PM",
"11/12/2019, 4:43:24 AM",
"11/12/2019, 4:49:39 PM"
]
I like to consider only the date in the json.
I tried code:
var cur;
var cnt = 0;
for (var i = 0; i < array_dates.length; i++) {
cur = current.getDate();
var d = new Date(array_dates[i]);
var n = d.getDate();
if (n != cur) {
if (cnt > 0) {
newdata[current] = cnt;
}
current = new Date(array_dates[i]);
cnt = 1;
} else {
cnt++;
}
}
newdata = JSON.stringify(newdata);
But I don't get an object in the expected way. I am missing something but not sure how to fix. How can I achieve the desired result with the array?