我正在尝试按营业时间对天数进行分组。
这就是我的对象this.customer.customerStoreStandardHours
的样子。
[
{
"dayOfWeek":"Monday",
"open":"8:00 AM",
"close":"5:00 PM"
},
{
"dayOfWeek":"Tuesday",
"open":"8:00 AM",
"close":"5:00 PM"
},
{
"dayOfWeek":"Wednesday",
"open":"8:00 AM",
"close":"6:00 PM"
},
{
"dayOfWeek":"Thursday",
"open":"8:00 AM",
"close":"5:00 PM"
},
{
"dayOfWeek":"Friday",
"open":"8:00 AM",
"close":"5:00 PM"
},
{
"dayOfWeek":"Saturday",
"open":"8:00 AM",
"close":"4:00 PM"
}
]
这是我到目前为止所拥有的:
storeGroupedHours() {
if (this.customer.customerStoreStandardHours) {
const groupedObject = this.customer.customerStoreStandardHours.reduce(function (accumulator, currentValue, i, array) {
const key = currentValue.open + currentValue.close
accumulator.ranges[key] = accumulator.ranges[key] || {};
accumulator.days[key] = accumulator.days[key] || [];
accumulator.ranges[key] = {
open: currentValue.open,
close: currentValue.close
};
accumulator.days[key].push({
index: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'].indexOf(currentValue.dayOfWeek),
name: currentValue.dayOfWeek
})
if (i == array.length - 1) {
for (let key in accumulator.ranges) {
if (accumulator.ranges.hasOwnProperty(key)) {
accumulator.days[key].sort(function(a, b) {
return a.index - b.index
});
if (accumulator.days[key][0].name === accumulator.days[key][accumulator.days[key].length - 1].name) {
accumulator.grouped.push({
days: [
accumulator.days[key][0].name,
].join(''),
open: accumulator.ranges[key].open,
close: accumulator.ranges[key].close
});
} else {
accumulator.grouped.push({
days: [
accumulator.days[key][0].name,
accumulator.days[key][accumulator.days[key].length - 1].name
].join('-'),
open: accumulator.ranges[key].open,
close: accumulator.ranges[key].close
});
}
}
}
}
return accumulator
}, {
ranges: {},
days: {},
grouped: [],
});
return groupedObject.grouped;
}
}
哪个产生了:
[
{
"days":"Monday-Friday",
"open":"8:00 AM",
"close":"5:00 PM"
},
{
"days":"Wednesday",
"open":"8:00 AM",
"close":"6:00 PM"
},
{
"days":"Saturday",
"open":"8:00 AM",
"close":"4:00 PM"
}
]
我真的很关门,但是问题是,如果商店在某个范围的中间有不同的营业时间,则该营业日不会中断,即星期三营业至6,但其他工作日营业至5。返回MF 9-5和W 9-6。所以在这种情况下,我会这样:
[
{
"days":"Monday-Tuesday",
"open":"8:00 AM",
"close":"5:00 PM"
},
{
"days":"Wednesday",
"open":"8:00 AM",
"close":"6:00 PM"
},
{
"days":"Thursday-Friday",
"open":"8:00 AM",
"close":"5:00 PM"
},
{
"days":"Saturday",
"open":"8:00 AM",
"close":"4:00 PM"
}
]
答案 0 :(得分:3)
您可能希望打开和关闭并添加一个新对象。否则,请更改days属性。
var data = [{ dayOfWeek: "Monday", open: "8:00 AM", close: "5:00 PM" }, { dayOfWeek: "Tuesday", open: "8:00 AM", close: "5:00 PM" }, { dayOfWeek: "Wednesday", open: "8:00 AM", close: "6:00 PM" }, { dayOfWeek: "Thursday", open: "8:00 AM", close: "5:00 PM" }, { dayOfWeek: "Friday", open: "8:00 AM", close: "5:00 PM" }, { dayOfWeek: "Saturday", open: "8:00 AM", close: "4:00 PM" }],
result = data.reduce((r, { dayOfWeek, open, close }) => {
if (!r.length || r[r.length - 1].open !== open || r[r.length - 1].close !== close)
r.push({ days: dayOfWeek, open, close });
else
r[r.length - 1].days = r[r.length - 1].days.split('-').slice(0, 1).concat(dayOfWeek).join('-');
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }