'How to fill in missing months of last year in an array using javascript
I have an array of months and its count as follows:
[
{
"registered_to_month": "04-2022",
"count": "3"
},
{
"registered_to_month": "03-2022",
"count": "4"
},
{
"registered_to_month": "02-2022",
"count": "6"
},
{
"registered_to_month": "01-2022",
"count": "6"
},
{
"registered_to_month": "11-2021",
"count": "11"
}
]
This result I am getting last 12 months The months which is not available I want to replace with 0 so that I can plot in the graph. I am not able to figure out how can I solve this.
Solution 1:[1]
We'll first generate an array of dates from the start date to the end date in the given data. Then compare which date exists in the given data. If a date exists in the given data, it is pushed to the result array else a new item is added to the result array with count as 0.
data = [
{
"registered_to_month": "04-2022",
"count": "3"
},
{
"registered_to_month": "03-2022",
"count": "4"
},
{
"registered_to_month": "02-2022",
"count": "6"
},
{
"registered_to_month": "01-2022",
"count": "6"
},
{
"registered_to_month": "11-2021",
"count": "11"
}
]
const generateAllDates = (startDate, endDate) =>{
var dates = [];
var startDateYr = startDate.split('-')[1];
var startDateMon = startDate.split('-')[0];
var endDateYr = endDate.split('-')[1];
var endDateMon = endDate.split('-')[0];
for(var year = startDateYr; year <= endDateYr; year++) {
for(var month = startDateMon; month <= 12; month++) {
dates.push(month.toString().padStart(2, "0")+"-"+year);
if (year >= endDateYr && month >= endDateMon) break;
};
startDateMon = 1;
};
return dates.reverse();
}
const fillMissingData = (data) =>{
// assuming the dates in data are arranged
// in descending order, sort first if otherwise
const startDate = data[data.length-1]["registered_to_month"]
const endDate = data[0]["registered_to_month"]
const allDates = generateAllDates(startDate, endDate)
var resultData = []
for (const genDate of allDates){
let exists = true
for(const item of data){
date = item["registered_to_month"]
if (genDate == date){
exists = true
resultData.push(item);
break
}else{
exists = false
}
}
if (!exists){
resultData.push({"registered_to_month": genDate, "count": '0'})
}
}
return resultData;
return resultData;
}
fillMissingData(data)
Solution 2:[2]
You can have an array of 12 elements with all values initially 0 and loop on all values you have. Add count to array as
var counts = new Array(12).fill(0);
var values = JSON.parse(your_string_with_json);
for (var i= 0; i<values.length; i++)
{
var month = values[i].registered_to_month.split('-')[0];
counts[parseInt(month)] = values[i].counts;
}
var allvalues;
for(var j= 0; j<counts.length; j++)
{
var val = {
registered_to_month: j<10 ? '0' + j : j,
count: counts[j]
};
allvalues.push(val);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | The Digerati |
| Solution 2 | Mesar ali |
