Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
Javascript Array = Adding in missing dates

I have some data of our bike shop sales throughout the day, and we are pushing that data through a loop to build up an array.

Our code looks like

var sales_array = [];
for (var i = 0; i < data_feed.length; i++)
{
    var sales_values = Object.values(data_feed[i]);
    sales_values = sales_values[0].split(";");
    var date = sales_values[0];

    var sales = sales_values[10];
    if(sales != '') { sales_array.push(date+','+sales); }
}

And this works great, with an output looking something like

"2022-02-07T16:20:00+00:00,20"
"2022-02-08T09:18:00+00:00,4500"
"2022-02-08T14:25:00+00:00,210"
"2022-02-09T11:21:00+00:00,100"

The problem is, if there's a couple of days when the shop is closed and no sales take place, we end up with missing dates in our output

For example, on the 27th Feb we were open, but on the 28th and 1st we were closed, and reopened on the 2nd, so we ended up with

"2022-02-27T16:45:00+00:00,3000"
"2022-02-27T17:10:00+00:00,450"
-----MISSING-----
"2022-03-02T08:29:00+00:00,1000"
"2022-03-02T15:54:00+00:00,550"

How can we change our loop code, so even if there is a date missing, it'll add it in with a 0 sale value, so the above would look like

"2022-02-27T16:45:00+00:00,3000"
"2022-02-27T17:10:00+00:00,450"
"2022-02-28T00:00:00+00:00,0"
"2022-03-01T00:00:00+00:00,0"
"2022-03-02T08:29:00+00:00,1000"
"2022-03-02T15:54:00+00:00,550"
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

add these code

Date.prototype.addDays = function(noOfDays){
    var tmpDate = new Date(this.valueOf());
    tmpDate.setDate(tmpDate.getDate() + noOfDays);
    return tmpDate;
}

var sort = sales_array.map(e => e.split(',')[0]).sort((a, b) => a.localeCompare(b))

for (var i = new Date(sort[0]).addDays(1); i < new Date(sort[sort.length-1]); i = i.addDays(1))
{
    sales_array.push(i.toISOString().split('T')[0]+'T00:00:00+00:00,0');
}

sales_array = sales_array.sort((a, b) => a.split(',')[0].localeCompare(b.split(',')[0]))

var data_feed = [
  { a: '2022-02-27T17:10:00+00:00;0;0;0;0;0;0;0;0;0;450' },
  { a: '2022-03-02T08:29:00+00:00;0;0;0;0;0;0;0;0;0;1000' }
]

var sales_array = [];
for (var i = 0; i < data_feed.length; i++)
{
    var sales_values = Object.values(data_feed[i]);
    sales_values = sales_values[0].split(";");
    var date = sales_values[0];

    var sales = sales_values[10];
    if(sales != '') { sales_array.push(date+','+sales); }
}

Date.prototype.addDays = function(noOfDays){
    var tmpDate = new Date(this.valueOf());
    tmpDate.setDate(tmpDate.getDate() + noOfDays);
    return tmpDate;
}

var sort = sales_array.map(e => e.split(',')[0]).sort((a, b) => a.localeCompare(b))

for (var i=new Date(sort[0]).addDays(1); i < new Date(sort[sort.length-1]); i=i.addDays(1))
{
    sales_array.push(i.toISOString().split('T')[0]+'T00:00:00+00:00,0');
}

sales_array = sales_array.sort((a,b) => a.split(',')[0].localeCompare(b.split(',')[0]))

console.log(sales_array);

about 4 years ago · Juan Pablo Isaza Report

0

asumming data_feed variable like

var data_feed = [
{value: "2022-02-07T16:20:00+00:00;;;;;;;;;;20"},
{value: "2022-02-08T09:18:00+00:00;;;;;;;;;;4500"},
{value: "2022-02-10T14:25:00+00:00;;;;;;;;;;210"},
{value: "2022-02-13T11:21:00+00:00;;;;;;;;;;100"}
]

i added some codes inside yours

var sales_array = [];
var next_current_ymd; // store expected 'next ymd' for compare with in next index
for (var i = 0; i < data_feed.length; i++)
{
    var sales_values = Object.values(data_feed[i]);
    sales_values = sales_values[0].split(";");
    var date = sales_values[0];
    
    // me added - start
    var current_ymd = date.substring(0,10);
    
    // next_current_ymd <= 'missing date' you explained < current_ymd
    // loop 'missing date' sequentially until current date
    // skipped first time because 'next_current_ymd' is null
    for(;next_current_ymd < current_ymd;){
        
        sales_array.push(next_current_ymd + 'T00:00:00+00:00,0');

        // next_current_ymd + 1
        var next_d = new Date(next_current_ymd);
        next_d.setUTCDate(next_d.getUTCDate() + 1);
        next_current_ymd = next_d.toISOString().substr(0,10);
    }
    
    // steor 'expected next ymd'
    var d = new Date(current_ymd);
    d.setUTCDate(d.getUTCDate() + 1);
    next_current_ymd = d.toISOString().substr(0,10);

    // me added - end
  
    var sales = sales_values[10];
    if(sales != '') { sales_array.push(date+','+sales); }
}
console.log(sales_array);

results:

[
    "2022-02-07T16:20:00+00:00,20",
    "2022-02-08T09:18:00+00:00,4500",
    "2022-02-09T00:00:00+00:00,0",
    "2022-02-10T14:25:00+00:00,210",
    "2022-02-11T00:00:00+00:00,0",
    "2022-02-12T00:00:00+00:00,0",
    "2022-02-13T11:21:00+00:00,100"
]
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!