1
I have an array with dates, each date means a new record in the users table, in this case, a new user account created. I need to go through this array to get the amount of accounts created over the last 12 months.
var arr = [“2018-02-19”, “2018-02-19”, “2018-02-19”, “2018-02-19”, “2018-02-19”, “2018-02-19”, “2018-02-19”, “2018-02-19”, “2018-03-23”, “2018-03-23”, “2019-06-12”, “2019-06-12”, “2019-06-15”, “2019-06-15”, “2019-06-15”, “2019-06-15”];
Result should be:
02/2018 -> 8 contas criadas
03/2018 -> 2 contas criadas
06/2019 -> 6 contas criadas
My code:
function setArray(array) {
var thisDate = array[0];
var count = 0;
var qtd = [];
var datas = [];
for (var i = 0; i < array.length; i++) {
if (array[i] == thisDate) {
count++;
}
if (array[i] != thisDate) {
datas = thisDate;
qtd = count;
count = 0;
}
thisDate = array[i];
}
console.log(datas);
console.log(qtd);
}
setArray(arr);
Note: The array is dynamic. So it always changes. After all, I want to get the monthly amount of records from the last 12 months.