Jignesh Vagh
Jignesh Vagh

Reputation: 120

I have json formated time slots and want to convert in proper time slots as written description

I have below JSON output from nodejs which is listed as below:

`{
  "data": {
    "2018-08-21": [ 
    { "available_slots": [ 
       { "slots": "1.15" },
       { "slots": "2:45,3:45" },
       { "slots": "4:30,5:30,6:30,7:30,8:30,9:30,10:30,11:30" }
       ] 
    }
    ]
  }
}`

And I want below output using nodejs:

`{ "available_slots": 
  [ { 
    "slot1": "2:45-3:45",
    "slot2": "4:30-5:30",
    "slot3": "5:30-6:30",
    "slot4": "6:30-7:30" } 
    ]
}`

I have coded below function to retrieve the above output but it gives only one slot data

function divideIntoSlots(slots) {
    
        var mainSlots = { 
                  "available_slots" : 
                    [ 
                     { "slot1" : "", "slot2" : "", "slot3" : "", "slot4" : "" 
                 } ] 
                 };
    
        var i = 1;
        slots.available_slots.forEach( function(slot) {
            var mainS = slot.slots;
            if(mainS.indexOf(',') > -1) {
                mainS = mainS.split(',');
                var newMain;
                if(i==1){
                    newMain = mainS[0]+"-"+mainS[1];
                    mainSlots.available_slots[0].slot1 = newMain;
                }
                i++;
            }
        });
    
        return mainSlots;
}

divideIntoSlots(slots)

OutPut:

`{ "available_slots": 
  [ { 
    "slot1": "2:45-3:45",
    "slot2": "",
    "slot3": "",
    "slot4": "" } 
    ]
}`

Anyone, please help me to short out this issue to resolve.

Upvotes: 0

Views: 834

Answers (2)

ymd
ymd

Reputation: 680

Probably you want that:

function divideIntoSlots(slots) {

  var mainSlots = {
    "available_slots": [{
      "slot1": "",
      "slot2": "",
      "slot3": "",
      "slot4": ""
    }]
  };

  let i = 1;
  slots.available_slots.forEach(function(slot) {
    var mainS = slot.slots;
    if (mainS.indexOf(',') > -1 && mainSlots.available_slots[0].hasOwnProperty('slot' + i)) {
      var newSlots = mainS.replace(/(\d+:\d+),(\d+:\d+)/g, "$1-$2")
      newSlots = newSlots.split(",");

      if (newSlots.length > 1) {
        newSlots.forEach(e => {
          if (!mainSlots.available_slots[0].hasOwnProperty("slot" + i))
            return;
          mainSlots.available_slots[0]["slot" + i] = e;
          i++;
        });
      } else {
        mainSlots.available_slots[0]["slot" + i] = newSlots[0];
        i++
      }
    }
  });

  return mainSlots;
}

console.log(divideIntoSlots({
  "available_slots": [{
      "slots": "1:15"
    },
    {
      "slots": "2:45,3:45"
    },
    {
      "slots": "4:30,5:30,6:30,7:30,8:30,9:30,10:30,11:30"
    }
  ]
}));

Upvotes: 1

Eddie
Eddie

Reputation: 26844

You can use reduce to summarise your data. Use forEach to loop thru the array,

var data = {"data":{"2018-08-21":[{"available_slots":[{"slots":"1.15"},{"slots":"2:45,3:45"},{"slots":"4:30,5:30,6:30,7:30,8:30,9:30,10:30,11:30"}]}]}}

function divideIntoSlots(slots) {
  var mainSlots = {"available_slots": []};

  var s = slots.available_slots.reduce((c, v) => {
    v.slots.split(',').forEach((o, i, a) => {
      if (c.length < 4 && a[i + 1]) c.push(o + "-" + a[i + 1]);
    });
    return c;
  }, []).reduce((c, v, i) => Object.assign(c, {["slot" + (i + 1)]: v}), {});

  mainSlots.available_slots.push(s);

  return mainSlots;
}

var result = divideIntoSlots(data.data['2018-08-21'][0])

console.log(result);

Upvotes: 2

Related Questions