I've an array which contains the data of users joined in last seven days from current date. e.g.
users=['19 Dec', '21 Dec', '21 Dec']
In this array, three users joined in last 7 days. I'm trying to find the unique occurence with this function.
`
let occurences = users.reduce(function (acc, curr) {
return acc[curr] ? acc[curr] : (acc[curr] = 1), acc;
}, {});
`
It's returning the object that contains the following key-pairs: {19 Dec: 1, 21 Dec: 2}
I'm expecting to fill the missing values in the object with 0 and with it's respective date. So, the final O/P will be : {19 Dec: 1, 20 Dec: 0, 21 Dec: 2, 22 Dec: 0, 23 Dec: 0, 24 Dec: 0, 25 Dec: 0}
Can someone please help me out to solve this?
I'm expecting to fill the missing values in the object with 0 and with it's respective date. So, the final O/P will be : {19 Dec: 1, 20 Dec: 0, 21 Dec: 2, 22 Dec: 0, 23 Dec: 0, 24 Dec: 0, 25 Dec: 0}
CodePudding user response:
Perhaps something like this will work for you. I'm using a modified version of the code found in this SO answer. The formatting was done thanks to this SO answer.
First, let's build an array containing the last 7 days.
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() days);
return date;
}
function getDates(startDate, stopDate) {
var dateArray = [];
var currentDate = startDate;
while (currentDate <= stopDate) {
var calendarDay = currentDate.getDate();
var monthName = new Intl.DateTimeFormat('en-US', options).format(currentDate);
dateArray.push(calendarDay " " monthName);
currentDate = currentDate.addDays(1);
}
return dateArray;
}
var options = { month: 'short' };
var end = new Date();
var start = end.addDays(-7);
var dateArray = getDates(start,end);
Next, let's assume that this is your JSON object with date - short month names as keys, and number of non-zero users as values.
let occurences = {
"19 Dec": 1,
"21 Dec": 2
};
We'll make a working copy of it, which we'll then modify as needed. You don't have to do this - you can declare occurences
differently, and modify it instead, but I'm doing it with the copy.
var copyOcc = occurences;
Next, we'll loop through our dateArray
, and check if there's a key in occurences
corresponding to the current value from dateArray
.
for(var i = 0; i < dateArray.length; i ) {
// If the key isn't there, add it
if(!copyOcc.hasOwnProperty(dateArray[i])) {
copyOcc[dateArray[i]] = 0;
}
}
console.log(copyOcc);
This will output:
{
18 Dec: 0,
19 Dec: 1,
20 Dec: 0,
21 Dec: 2,
22 Dec: 0,
23 Dec: 0,
24 Dec: 0,
25 Dec: 0
}
See the following fiddle for the complete script.
CodePudding user response:
Same as above answer but in pure javascript.
const users = ['19 Dec', '21 Dec', '21 Dec']
const month_to_abv = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
// fetch previouse days, default it fetches 7 days
function getPreviouseDaysObj(noOfDays = 7) {
const obj = {};
for (let i = 0; i < noOfDays; i ) {
const curr_date = new Date();
curr_date.setDate(curr_date.getDate() - i);
obj[formatDate(curr_date)] = 0;
}
return obj;
}
// format date
function formatDate(date) {
return `${date.getDate()} ${month_to_abv[date.getMonth()]}`
}
// create obj for previouse 7 days
const previouse_days = getPreviouseDaysObj(7);
// calculate occurance
const occurences = users.reduce(function (acc, curr) {
acc[curr] ? acc[curr] : (acc[curr] = 1);
return acc;
}, Object.assign({}, previouse_days));
// Log to console
console.log(occurences)
CodePudding user response:
You could try the following:
- Create an array containing the last 7 days, formatted according to the dates in
users
-array - Iterate through the last 7 dates and apply
filter
for each day to find out how many users joined on the given day
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
ngOnInit(): void {
const users = ['19 Dec', '21 Dec', '21 Dec'];
const occurences = this.getLastXDates(7).map(d => {
const numbOfUsers = users.filter(u => u === this.formatDate(d)).length;
return `${this.formatDate(d)}: ${numbOfUsers}`;
});
console.log('Unique visits', occurences);
}
getLastXDates(numbOfDays: number): Date[] {
return Array.from(Array(numbOfDays).keys()).reverse().map(i => {
const d = new Date();
d.setDate(d.getDate() - i);
return d;
});
}
formatDate(d: Date): string {
const day = d.getDate();
const month = this.months[d.getMonth()];
return `${day} ${month}`;
}