从阵列获取一天中的最低和最高时间

时间:2019-10-04 06:12:28

标签: javascript node.js momentjs

我有这样一个对象数组

const timings = [{
        monday: { from: "12:00", to: "13:00" },
        tuesday: { from: "11:00", to: "13:00" },
        thursday: { from: "11:00", to: "13:00" },
        friday: { from: "11:00", to: "13:00" },
        saturday: { from: "11:00", to: "13:00" },
    },

    {
        monday: { from: "10:00", to: "17:00" },
        tuesday: { from: "09:00", to: "10:00" },
        thursday: { from: "05:00", to: "13:00" },
        friday: { from: "02:00", to: "13:30" },
        saturday: { from: "11:00", to: "13:00" },
    },

    {
        monday: { from: "13:00", to: "14:20" },
        tuesday: { from: "11:00", to: "13:00" },
        thursday: { from: "11:00", to: "13:00" },
        friday: { from: "11:00", to: "13:00" },
        saturday: { from: "11:00", to: "13:00" },
    },

    {
        monday: { from: "12:00", to: "13:00" },
        tuesday: { from: "11:00", to: "13:40" },
        thursday: { from: "11:00", to: "16:00" },
        friday: { from: "11:00", to: "13:00" },
        saturday: { from: "11:00", to: "13:00" },
        sunday: {from: "00:00", to: "23:59"}
    },
]

我希望输出包含数组中当天的最低和最高时间

例如输出:

{
    monday: { from: "10:00", to: "17:00" },
    tuesday: { from: "09:00", to: "13:40" },
    thursday: { from: "05:00", to: "16:00" },
    friday: { from: "02:00", to: "13:30" },
    saturday: { from: "11:00", to: "13:00" },
    sunday: {from: "00:00", to: "23:59"}
}

我想出了一个很长的解决方案,但是它真的很长而且效率不高。

我的解决方案

mondayFrom = timings.map(d => (d.monday && d.monday.from))
mondayTo = timings.map(d => (d.monday && d.monday.to))
tuesdayFrom = timings.map(d => (d.tuesday && d.tuesday.to))
// .... rest of the days

这将生成一个数组,我可以从该数组中找到最小值和最大值,但是我觉得这是一个不好的逻辑。

如果有更好的方法,请告诉我。 谢谢

2 个答案:

答案 0 :(得分:3)

使用reduce使其相当简单

const timings = [{
        monday: { from: "12:00", to: "13:00" },
        tuesday: { from: "11:00", to: "13:00" },
        thursday: { from: "11:00", to: "13:00" },
        friday: { from: "11:00", to: "13:00" },
        saturday: { from: "11:00", to: "13:00" },
    },

    {
        monday: { from: "10:00", to: "17:00" },
        tuesday: { from: "09:00", to: "10:00" },
        thursday: { from: "05:00", to: "13:00" },
        friday: { from: "02:00", to: "13:30" },
        saturday: { from: "11:00", to: "13:00" },
    },

    {
        monday: { from: "13:00", to: "14:20" },
        tuesday: { from: "11:00", to: "13:00" },
        thursday: { from: "11:00", to: "13:00" },
        friday: { from: "11:00", to: "13:00" },
        saturday: { from: "11:00", to: "13:00" },
    },

    {
        monday: { from: "12:00", to: "13:00" },
        tuesday: { from: "11:00", to: "13:40" },
        thursday: { from: "11:00", to: "16:00" },
        friday: { from: "11:00", to: "13:00" },
        saturday: { from: "11:00", to: "13:00" },
        sunday: {from: "00:00", to: "23:59"}
    },
]

let result = timings.reduce((a, x) => {
    Object.entries(x).forEach(([dow, obj]) => {
        if(!a[dow]) {
            a[dow] = Object.assign({}, obj);
        } else {
            a[dow].from = a[dow].from < obj.from ? a[dow].from : obj.from;
            a[dow].to = a[dow].to > obj.to ? a[dow].to : obj.to;
        }
    });
    return a;
}, {});

console.log(result);

请注意:a[dow] = Object.assign({}, obj);,因此原始对象不会被突变

答案 1 :(得分:2)

以下是一个使用.reduce的示例:

const timings = [{
        monday: { from: "12:00", to: "13:00" },
        tuesday: { from: "11:00", to: "13:00" },
        thursday: { from: "11:00", to: "13:00" },
        friday: { from: "11:00", to: "13:00" },
        saturday: { from: "11:00", to: "13:00" },
    },

    {
        monday: { from: "10:00", to: "17:00" },
        tuesday: { from: "09:00", to: "10:00" },
        thursday: { from: "05:00", to: "13:00" },
        friday: { from: "02:00", to: "13:30" },
        saturday: { from: "11:00", to: "13:00" },
    },

    {
        monday: { from: "13:00", to: "14:20" },
        tuesday: { from: "11:00", to: "13:00" },
        thursday: { from: "11:00", to: "13:00" },
        friday: { from: "11:00", to: "13:00" },
        saturday: { from: "11:00", to: "13:00" },
    },

    {
        monday: { from: "12:00", to: "13:00" },
        tuesday: { from: "11:00", to: "13:40" },
        thursday: { from: "11:00", to: "16:00" },
        friday: { from: "11:00", to: "13:00" },
        saturday: { from: "11:00", to: "13:00" },
        sunday: {from: "00:00", to: "23:59"}
    },
];

const formatTimings = function(timings) {

  return timings.reduce(function(result, timing) {
    Object.keys(timing).forEach(function(key) {
      result[key] = {
        from: !result[key] 
          ? timing[key].from 
          : [result[key].from, timing[key].from].sort()[0],
        to: !result[key]
          ? timing[key].to
          : [result[key].to, timing[key].to].sort().reverse()[0]
      };
    })
    return result
  }, {})

}

console.log(formatTimings(timings));