如何格式化嵌套 json 数组取决于条件请求 json for api
How to format neasted json array depends on conditions request json for api
如何格式化 neasted json 数组取决于条件我有表单请求 json for api
这是我的前端json数组
我使用 angular 反应形式
创建了这个数组
但 Api 需要 requires 而不是空字段
在上面的数组中嵌套了 days->timings->breaks
条件
1) 如果天数不包含时间,则无需提交(例如:第 3 天不包含时间,因此无需提交)
2) 如果街区、楼层、房间都是空的,则无需登顶(示例:day[1]-timing[1]、timing[2]、day[2]-timing[2])
3) 如果 breaks 为空则不需要提交 breaks array in days(example: day1[1]-> timings[3], day[2]->timings[2])
4
) in breks if mode is true (duration, quantity), if mode is true
(start time, end times are required
5) api 无需嵌套(只需要普通数组)(例如:[{}, {}, {}])
通过以上条件,你可以假设这个数组
我试过用for循环来解决,但是重复的东西不太好
前端数组
{
"startDate":"10-05-2018",
"endDate":"13-05-2018",
"days":[
{
"dayId":1,
"timings":[
{
"startTime":"10:00",
"endTime":"12:00",
"cycle":1,
"blocks":1,
"floors":2,
"rooms":3,
"breaks":[
{
"type":1,
"mode":false,
"startTime":"01:00",
"endTime":"02:00",
"duration":1,
"quantity":1
},
{
"type":1,
"mode":true,
"startTime":"01:00",
"endTime":"02:00",
"duration":1,
"quantity":1
}
]
},
{
"startTime":"10:00",
"endTime":"12:00",
"cycle":1,
"blocks":"",
"floors":"",
"rooms":"",
"breaks":[
{
"type":1,
"mode":false,
"startTime":"01:00",
"endTime":"02:00",
"duration":1,
"quantity":1
}
]
},
{
"startTime":"10:00",
"endTime":"12:00",
"cycle":1,
"blocks":"",
"floors":"",
"rooms":"",
"breaks":[
]
},
{
"startTime":"10:00",
"endTime":"12:00",
"cycle":1,
"blocks":2,
"floors":3,
"rooms":3,
"breaks":[
]
}
]
},
{
"dayId":2,
"timings":[
{
"startTime":"10:00",
"endTime":"12:00",
"cycle":1,
"blocks":1,
"floors":2,
"rooms":3,
"breaks":[
{
"type":1,
"mode":true,
"startTime":"01:00",
"endTime":"02:00",
"duration":1,
"quantity":1
}
]
},
{
"startTime":"10:00",
"endTime":"12:00",
"cycle":1,
"blocks":"",
"floors":"",
"rooms":"",
"breaks":[
]
}
]
},
{
"dayId":3,
"timings":[
]
}
]
}
请求数组
{
"start_date":"05-05-2018",
"end_date":"31-07-2018",
"branch_id":"2",
"day":[
{
"id":"1",
"start_time":"10:00",
"end_time":"12:00",
"breaks":[
{
"type":1,
"mode":false,
"duration":1,
"quantity":1
},
{
"type":1,
"mode":true,
"startTime":"01:00",
"endTime":"02:00",
}
],
"gen_repeat_cycle_id":"1",
"room_id":"1",
"floor_id":"2",
"block_id":"3"
},
{
"id":"1",
"start_time":"10:00",
"end_time":"12:00",
"breaks":[
{
"type":1,
"mode":false,
"duration":1,
"quantity":1
}
],
"gen_repeat_cycle_id":"2"
},
{
"id":"1",
"start_time":"10:00",
"end_time":"12:00",
"gen_repeat_cycle_id":"1"
},
{
"id":"1",
"start_time":"10:00",
"end_time":"12:00",
"gen_repeat_cycle_id":"1",
"room_id":"1",
"floor_id":"2",
"block_id":"3"
},
{
"id":"2",
"start_time":"10:00",
"end_time":"12:00",
"breaks":[
{
"type":1,
"mode":true,
"startTime":"01:00",
"endTime":"02:00",
}
],
"gen_repeat_cycle_id":"1",
"room_id":"1",
"floor_id":"2",
"block_id":"3"
},
{
"id":"2",
"start_time":"10:00",
"end_time":"12:00",
"gen_repeat_cycle_id":"1"
}
]
}
请帮帮我
(更新)
这是我想出的解决方案
function processData(data) {
var newData = {};
newData.start_date = data.startDate;
newData.end_date = data.endDate;
newData.day = [];
for(var i = 0; i < data.days.length; i++) {
var currentDay = data.days[i];
for(var j = 0; j < currentDay.timings.length; j++) {
var currentTiming = currentDay.timings[j];
var newTimingObject = {};
newTimingObject.start_time = currentTiming.startTime;
newTimingObject.end_time = currentTiming.endTime;
newTimingObject.gen_repeat_cycle_id = currentTiming.cycle.toString();
newTimingObject.id = currentDay.dayId.toString();
if(currentTiming.breaks.length > 0) {
var currentBreaks = [];
for(var k = 0; k < currentTiming.breaks.length; k++) {
var newBreakObject = {};
newBreakObject.type = currentTiming.breaks[k].type;
newBreakObject.mode = currentTiming.breaks[k].mode;
if(currentTiming.breaks[k].mode === true) {
newBreakObject.startTime = currentTiming.breaks[k].startTime;
newBreakObject.endTime = currentTiming.breaks[k].endTime;
} else {
newBreakObject.duration = currentTiming.breaks[k].duration;
newBreakObject.quantity = currentTiming.breaks[k].quantity;
}
currentBreaks.push(newBreakObject);
}
newTimingObject.breaks = currentBreaks;
}
if(currentTiming.blocks !== '') {
newTimingObject.block_id = currentTiming.blocks.toString();
}
if(currentTiming.floors !== '') {
newTimingObject.floor_id = currentTiming.floors.toString();
}
if(currentTiming.rooms !== '') {
newTimingObject.room_id = currentTiming.rooms.toString();
}
newData.day.push(newTimingObject);
}
}
return newData;
}
我在 jsfiddle 中找到了解决方案,请查看,但我不明白所请求数组中的 branch_id。
如何格式化 neasted json 数组取决于条件我有表单请求 json for api
这是我的前端json数组 我使用 angular 反应形式
创建了这个数组但 Api 需要 requires 而不是空字段
在上面的数组中嵌套了 days->timings->breaks
条件
1) 如果天数不包含时间,则无需提交(例如:第 3 天不包含时间,因此无需提交)
2) 如果街区、楼层、房间都是空的,则无需登顶(示例:day[1]-timing[1]、timing[2]、day[2]-timing[2])
3) 如果 breaks 为空则不需要提交 breaks array in days(example: day1[1]-> timings[3], day[2]->timings[2])
4
) in breks if mode is true (duration, quantity), if mode is true (start time, end times are required
5) api 无需嵌套(只需要普通数组)(例如:[{}, {}, {}])
通过以上条件,你可以假设这个数组
我试过用for循环来解决,但是重复的东西不太好
前端数组
{
"startDate":"10-05-2018",
"endDate":"13-05-2018",
"days":[
{
"dayId":1,
"timings":[
{
"startTime":"10:00",
"endTime":"12:00",
"cycle":1,
"blocks":1,
"floors":2,
"rooms":3,
"breaks":[
{
"type":1,
"mode":false,
"startTime":"01:00",
"endTime":"02:00",
"duration":1,
"quantity":1
},
{
"type":1,
"mode":true,
"startTime":"01:00",
"endTime":"02:00",
"duration":1,
"quantity":1
}
]
},
{
"startTime":"10:00",
"endTime":"12:00",
"cycle":1,
"blocks":"",
"floors":"",
"rooms":"",
"breaks":[
{
"type":1,
"mode":false,
"startTime":"01:00",
"endTime":"02:00",
"duration":1,
"quantity":1
}
]
},
{
"startTime":"10:00",
"endTime":"12:00",
"cycle":1,
"blocks":"",
"floors":"",
"rooms":"",
"breaks":[
]
},
{
"startTime":"10:00",
"endTime":"12:00",
"cycle":1,
"blocks":2,
"floors":3,
"rooms":3,
"breaks":[
]
}
]
},
{
"dayId":2,
"timings":[
{
"startTime":"10:00",
"endTime":"12:00",
"cycle":1,
"blocks":1,
"floors":2,
"rooms":3,
"breaks":[
{
"type":1,
"mode":true,
"startTime":"01:00",
"endTime":"02:00",
"duration":1,
"quantity":1
}
]
},
{
"startTime":"10:00",
"endTime":"12:00",
"cycle":1,
"blocks":"",
"floors":"",
"rooms":"",
"breaks":[
]
}
]
},
{
"dayId":3,
"timings":[
]
}
]
}
请求数组
{
"start_date":"05-05-2018",
"end_date":"31-07-2018",
"branch_id":"2",
"day":[
{
"id":"1",
"start_time":"10:00",
"end_time":"12:00",
"breaks":[
{
"type":1,
"mode":false,
"duration":1,
"quantity":1
},
{
"type":1,
"mode":true,
"startTime":"01:00",
"endTime":"02:00",
}
],
"gen_repeat_cycle_id":"1",
"room_id":"1",
"floor_id":"2",
"block_id":"3"
},
{
"id":"1",
"start_time":"10:00",
"end_time":"12:00",
"breaks":[
{
"type":1,
"mode":false,
"duration":1,
"quantity":1
}
],
"gen_repeat_cycle_id":"2"
},
{
"id":"1",
"start_time":"10:00",
"end_time":"12:00",
"gen_repeat_cycle_id":"1"
},
{
"id":"1",
"start_time":"10:00",
"end_time":"12:00",
"gen_repeat_cycle_id":"1",
"room_id":"1",
"floor_id":"2",
"block_id":"3"
},
{
"id":"2",
"start_time":"10:00",
"end_time":"12:00",
"breaks":[
{
"type":1,
"mode":true,
"startTime":"01:00",
"endTime":"02:00",
}
],
"gen_repeat_cycle_id":"1",
"room_id":"1",
"floor_id":"2",
"block_id":"3"
},
{
"id":"2",
"start_time":"10:00",
"end_time":"12:00",
"gen_repeat_cycle_id":"1"
}
]
}
请帮帮我
(更新) 这是我想出的解决方案
function processData(data) {
var newData = {};
newData.start_date = data.startDate;
newData.end_date = data.endDate;
newData.day = [];
for(var i = 0; i < data.days.length; i++) {
var currentDay = data.days[i];
for(var j = 0; j < currentDay.timings.length; j++) {
var currentTiming = currentDay.timings[j];
var newTimingObject = {};
newTimingObject.start_time = currentTiming.startTime;
newTimingObject.end_time = currentTiming.endTime;
newTimingObject.gen_repeat_cycle_id = currentTiming.cycle.toString();
newTimingObject.id = currentDay.dayId.toString();
if(currentTiming.breaks.length > 0) {
var currentBreaks = [];
for(var k = 0; k < currentTiming.breaks.length; k++) {
var newBreakObject = {};
newBreakObject.type = currentTiming.breaks[k].type;
newBreakObject.mode = currentTiming.breaks[k].mode;
if(currentTiming.breaks[k].mode === true) {
newBreakObject.startTime = currentTiming.breaks[k].startTime;
newBreakObject.endTime = currentTiming.breaks[k].endTime;
} else {
newBreakObject.duration = currentTiming.breaks[k].duration;
newBreakObject.quantity = currentTiming.breaks[k].quantity;
}
currentBreaks.push(newBreakObject);
}
newTimingObject.breaks = currentBreaks;
}
if(currentTiming.blocks !== '') {
newTimingObject.block_id = currentTiming.blocks.toString();
}
if(currentTiming.floors !== '') {
newTimingObject.floor_id = currentTiming.floors.toString();
}
if(currentTiming.rooms !== '') {
newTimingObject.room_id = currentTiming.rooms.toString();
}
newData.day.push(newTimingObject);
}
}
return newData;
}
我在 jsfiddle 中找到了解决方案,请查看,但我不明白所请求数组中的 branch_id。