我有json格式的时隙,并希望在正确的时隙中进行转换,如书面说明所述

时间:2018-08-21 14:31:00

标签: javascript json node.js

我在下面列出了nodejs的JSON输出,如下所示:

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

我想在下面使用nodejs输出:

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

我已经对以下函数进行了编码,以检索上述输出,但它仅提供一个插槽数据

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)

输出:

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

任何人,请帮助我简化此问题以解决。

2 个答案:

答案 0 :(得分:2)

您可以使用reduce来汇总数据。使用forEach遍历数组,

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

答案 1 :(得分:1)

可能您想要这样:

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"
    }
  ]
}));