Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在计算丢失的项后向JSON对象添加新数组_Javascript_Jquery_Json - Fatal编程技术网

Javascript 在计算丢失的项后向JSON对象添加新数组

Javascript 在计算丢失的项后向JSON对象添加新数组,javascript,jquery,json,Javascript,Jquery,Json,我有以下json文件 [{"id":5,"num":"n61","mov_date":"2019-02-01T00:00:00","orders":19}, {"id":5,"num":"n61","mov_date":"2019-02-05T00:00:00","orders":12}, {"id":5,"num":"n61","mov_date":"2019-02-08T00:00:00","orders":5}, {"id":5,"num":"n61","mov_date":"2019-0

我有以下json文件

[{"id":5,"num":"n61","mov_date":"2019-02-01T00:00:00","orders":19},
{"id":5,"num":"n61","mov_date":"2019-02-05T00:00:00","orders":12},
{"id":5,"num":"n61","mov_date":"2019-02-08T00:00:00","orders":5},
{"id":5,"num":"n61","mov_date":"2019-02-11T00:00:00","orders":7}]
我想使用JavaScript jquery添加新项,以

[{"id":5,"num":"n61","mov_date":"2019-02-01T00:00:00","orders":19},
{"id":5,"num":"n61","mov_date":"2019-02-02T00:00:00","orders":0},
{"id":5,"num":"n61","mov_date":"2019-02-03T00:00:00","orders":0},
{"id":5,"num":"n61","mov_date":"2019-02-04T00:00:00","orders":0},
{"id":5,"num":"n61","mov_date":"2019-02-05T00:00:00","orders":12},
{"id":5,"num":"n61","mov_date":"2019-02-06T00:00:00","orders":0},
{"id":5,"num":"n61","mov_date":"2019-02-07T00:00:00","orders":0},
{"id":5,"num":"n61","mov_date":"2019-02-08T00:00:00","orders":5},
{"id":5,"num":"n61","mov_date":"2019-02-09T00:00:00","orders":0},
{"id":5,"num":"n61","mov_date":"2019-02-10T00:00:00","orders":0},
{"id":5,"num":"n61","mov_date":"2019-02-11T00:00:00","orders":7}]
可能通过计算日期之间遗漏项目的数量,或者只计算数字之间代表日期的差异,即:“2019-02-
01
T00:00:00”和“2019-02-
05
T00:00:00”,然后添加3个项目?

var项目=[
{“id”:5,“num”:“n61”,“mov_date”:“2019-02-01T00:00:00”,“orders”:19},
{“id”:5,“num”:“n61”,“mov_date”:“2019-02-05T00:00:00”,“orders”:12},
{“id”:5,“num”:“n61”,“mov_date”:“2019-02-08T00:00:00”,“orders”:5},
{“id”:5,“num”:“n61”,“mov_date”:“2019-02-11T00:00:00”,“orders”:7}
]
var newItems=[]
对于(变量i=0;i对于(var j=1;j您可以使用一个
Date
对象,您可以使用其
setDate
方法递增该对象,并使用
toJSON
将其呈现为字符串。然后,当日期字符串与下一个条目匹配时,您可以复制它,否则,您可以使用
orders:0
复制它:

const data=[{“id”:5,“num”:“n61”,“mov_date”:“2019-02-01T00:00:00”,“orders”:19},{“id”:5,“num”:“n61”,“mov_date”:“2019-02-05T00:00:00”,“orders”:12},{“id”:5,“num”:“n61”,“mov_date”:“2019-02-08T00:00:00”,“orders”:5},{“id”:5,“num”:“n61”,“mov_date”:“2019-02-11:00:00”,“orders”:;
const end=新日期(数据[data.length-1].mov_Date+“Z”);
常量结果=[];

对于(让dt=newdate(data[0].mov_Date+“Z”),i=0;dt也检查这个使用
reduce()
保留所有道具(包括
id
)的道具,并且只重置
订单
并在两者之间设置正确的
mov_Date

var项目=[{
“id”:5,
“num”:“n61”,
“移动日期”:“2019-02-01T00:00:00”,
“命令”:19
},
{
“id”:5,
“num”:“n61”,
“移动日期”:“2019-02-05T00:00:00”,
“命令”:12
},
{
“id”:5,
“num”:“n61”,
“移动日期”:“2019-02-08T00:00:00”,
“命令”:5
},
{
“id”:5,
“num”:“n61”,
“移动日期”:“2019-02-11T00:00:00”,
“命令”:7
}
]
const newItems=items.reduce((acc,next)=>{
//第一次运行,提前返回
如果(!根据长度){
返回[…acc,next]
}
//使用最近的物品,以保留id和其他道具
const previitem=附件[附件长度-1];
//在几天内获得差异-1
const days=time.utc(next.mov_date).diff(time.utc(previitem.mov_date),“days”)-1;
//[…数组]是一种获得无数组孔的可映射数组的技巧,
//但对于初始化的未定义值,
//所以我们可以在映射过程中获取索引
常量中间值=[…数组(天)].map((\ux,dayIndex)=>{
返回{
…前一项,
订单:0,
mov_date:moment.utc(previitem.mov_date).add(dayIndex+1,'days')。格式()
};
});
//合并所有内容,并移动到下一个循环
返回[…acc,…中间值,下一个];
}, [])
console.log(newItems);

这里有一个算法可以帮你做到这一点 用于日期转换

function twoDigits(d) {
    if(0 <= d && d < 10) return "0" + d.toString();
    if(-10 < d && d < 0) return "-0" + (-1*d).toString();
    return d.toString();
}

function toMysqlFormat() {
    return this.getUTCFullYear() + "-" + twoDigits(1 + this.getUTCMonth()) + "-" + twoDigits(this.getUTCDate()) + "T" + twoDigits(this.getUTCHours()) + ":" + twoDigits(this.getUTCMinutes()) + ":" + twoDigits(this.getUTCSeconds());
};


var prev = 0;

for( var x = 1; x < obj.length; x++ ){

    if( !obj[x -1].mov_date ){
        continue;
    }

    var tx = Date.parse( obj[x-1].mov_date );
    var diff = ( Date.parse(obj[x].mov_date ) - tx ) / (1000*24*60*60);

    for( var y = 1; y < diff; y++ ){

        obj.splice( x - 1 + y,0, { "id" : 5, "num" : "n61", "mov_date" : toMysqlFormat.bind( new Date( tx + ( y*1000*24*60*60) ) )(), "orders" : 0} );

    }
    x += diff - 1;

}

for( var x = 0; x < obj.length; x++ ){
    console.log( JSON.stringify( obj[x] ) );
}

/* Result :
/* {"id":5,"num":"n61","mov_date":"2019-02-01T00:00:00","orders":19} */
/* {"id":5,"num":"n61","mov_date":"2019-02-02T00:00:00","orders":0} */
/* {"id":5,"num":"n61","mov_date":"2019-02-03T00:00:00","orders":0} */
/* {"id":5,"num":"n61","mov_date":"2019-02-04T00:00:00","orders":0} */
/* {"id":5,"num":"n61","mov_date":"2019-02-05T00:00:00","orders":12} */
/* {"id":5,"num":"n61","mov_date":"2019-02-06T00:00:00","orders":0} */
/* {"id":5,"num":"n61","mov_date":"2019-02-07T00:00:00","orders":0} */
/* {"id":5,"num":"n61","mov_date":"2019-02-08T00:00:00","orders":5} */
/* {"id":5,"num":"n61","mov_date":"2019-02-09T00:00:00","orders":0} */
/* {"id":5,"num":"n61","mov_date":"2019-02-10T00:00:00","orders":0} */
/* {"id":5,"num":"n61","mov_date":"2019-02-11T00:00:00","orders":7} */
*/
功能两位数(d){

如果(0)如果
id
值不同怎么办?感谢您的回答,但是如果您注意到有错误,即前3个插入项目的日期不正确:“2019-02-02T22:00:00Z”、“2019-02-
04
T22:00:00Z”、“2019-02-
07
T22:00:00Z”,而它们必须是“2019-02-02T22:00:00Z”,“2019-02
-03
T22:00:00Z”,“2019-02-
04
T22:00:00Z”。等等,我已经更正了我的代码片段。现在一切都按预期进行。