I tried out solutions: here and here but my problem has some additional complications - comparing dates.
Problem: count duplicate Date() objects in JS array. Date objects have different time, but whats important is that date, month and year are the same. For example 3\3\2020 14:30 and 3\3\202 12:21 should be counted as same date.
Dataset(input):
let dates = [new Date(2018, 11, 24, 22), new Date(2018, 12, 23), new Date(2013, 02, 3),
new Date(2018, 11, 24, 22), new Date(2018, 12, 23, 22), new Date(2013, 02, 3, 22)]
expected output:
var result = [
{ 'date': dateobj0 , 'count':3; },
{ 'date': dateobj1 , 'count':4; },
{ 'date': dateobj2 , 'count':10; },
...
{ 'date': dateobjn , 'count':n; },
]
I started by mapping them:
var result = {'date':'', 'count':0};
let mapedDatesList= dates.map(x => {result['date'] = x; return result} )
And after that I tried out to modify\implement solutions listed above with no success. Best I've got is some bubble sort situation, that does not work... . Can someone offer more elegant solution with forEach or map?
let mapedDatesList= dates.map(x => {result['date'] = x; return result} )
console.log(mapedDatesList)
mapedDatesList.forEach( (x, i) => x.count != 0 ? mapedDatesList[i].count++: x.count++ )
console.log('-----')
console.log(mapedDatesList)
Totally got stuck in tunnel vision. This situation just counts every time it itterates in forEach in every single object.