我有一个原始的“条目”和“退出”数据作为json for person(按日期排序)。
[{
id: 1,
dateTime: "2015-11-26T16:30:48.000Z",
gateNumber: "192.168.0.202",
gateType: "entry",
personId: 207
}, // the exit object is missing for id=1. I need to create same object as exit vice versa...
{
id: 2,
dateTime: "2015-11-26T16:35:44.000Z",
gateNumber: "192.168.0.201",
gateType: "entry",
personId: 207
}, {
id: 3,
dateTime: "2015-11-26T16:36:40.000Z",
gateNumber: "192.168.0.202",
gateType: "exit",
personId: 207
}, {
id: 4,
dateTime: "2015-11-26T16:37:22.000Z",
gateNumber: "192.168.0.201",
gateType: "entry",
personId: 207
}, {
id: 5,
dateTime: "2015-11-26T16:38:55.000Z",
gateNumber: "192.168.0.201",
gateType: "exit",
personId: 207
}]
它需要像入门一样 - >退出 - >进入 - >出口。 但有时会丢失一些“退出”或某些“入口”对象。
所以我试图检测丢失的“对象”并为批量插入创建一个数组,因为差异将是0秒并且会失败。
所以,解决方案应该适用于以上情况;
[
[1, "2015-11-26T16:30:48.000Z", "192.168.0.202", "exit", 207],
//... if there are any more..
]
答案 0 :(得分:-1)
var json_obj = [{
id: 1,
dateTime: "2015-11-26T16:30:48.000Z",
gateNumber: "192.168.0.202",
gateType: "entry",
personId: 207
},
{
id: 2,
dateTime: "2015-11-26T16:35:44.000Z",
gateNumber: "192.168.0.201",
gateType: "entry",
personId: 207
}, {
id: 3,
dateTime: "2015-11-26T16:36:40.000Z",
gateNumber: "192.168.0.202",
gateType: "exit",
personId: 207
}, {
id: 4,
dateTime: "2015-11-26T16:37:22.000Z",
gateNumber: "192.168.0.201",
gateType: "entry",
personId: 207
}, {
id: 5,
dateTime: "2015-11-26T16:38:55.000Z",
gateNumber: "192.168.0.201",
gateType: "exit",
personId: 207
}];
your_new_array = json_obj.map(function(obj){
if(!obj.hasOwnProperty('exit'))
{
obj.exit = 'exit value';
}
if(!obj.hasOwnProperty('entry'))
{
obj.entry = 'entry value';
}
return obj;
});
console.log(your_new_array);
https://jsfiddle.net/4f5Lpcge/小提琴:)我希望它有所帮助!