0

How to format neasted json array depends on conditions I have form request json for api

This is my front end json array I created this array using angular reactive forms

But Api need requires and not empty fields only

In above array is nested days->timings->breaks

conditions

1) if days not contain timings no need to sumbit(example: day-3 not contain timings so no need to submit)

2) if blocks, floors, room are empty no need to summit(example: day[1]-timing[1], timing[2], day[2]-timing[2])

3) if breaks are empty not need to submit the 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 no need nesting(just need normal array)(example:[{}, {}, {}])

By floowing above conditions u can assume this array

I tried using for loops to solve it, but it's duplicate the things not well

Front end array

    {  
   "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":[  

         ]
      }
   ]
}

requested array

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

Please help me

1 Answer 1

2

(Updated) This is the solution I came up with

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

I have the solution in jsfiddle check it out and I don't understand branch_id in the requested array.

Sign up to request clarification or add additional context in comments.

2 Comments

You r solution is almost working , breaks array I can't add directly there is some conditions, can you please check the above edited question
Corrected the function to match your requested array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.