Javascript 数组 = 添加缺失的日期

发布于 2025-01-14 01:11:46 字数 1105 浏览 1 评论 0原文

我有一些自行车店全天销售的数据,我们正在通过循环推送这些数据来构建一个数组。

我们的代码看起来像

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); }
}

这样效果很好,输出看起来像这样

"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"

问题是,如果有几天商店关门并且没有进行销售,我们最终会在输出中丢失日期

例如,在2 月 27 日我们是开放的,但在 28 日和 1 日我们关闭了,并在 2 日重新开放,所以我们最终得到了

"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"

如何更改循环代码,所以即使缺少日期,它也会将其添加到与 0销售价值,所以上面看起来像

"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"

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无风消散 2025-01-21 01:11:46

假设 data_feed 变量就像

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"}
]

中添加了一些代码:

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);

我在你的结果

[
    "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"
]

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"
]
酒废 2025-01-21 01:11:46

添加这些代码

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);

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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文