[Solved] Fetching Lowest and Highest Time of the Day from Array


using reduce makes it fairly simple

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

note: a[dow] = Object.assign({}, obj); so the original object won’t get mutated

solved Fetching Lowest and Highest Time of the Day from Array