如何在javascript中根据日期修改对象数组



下面有一组可用插槽。

[
{ date: "06/10/2020", start: "10:00 am", end: "11:00 am" },
{ date: "06/10/2020", start: "11:00 am", end: "12:00 am" },
{ date: "06/10/2020", start: "12:00 am", end: "01:00 pm" },
{ date: "07/10/2020", start: "10:00 am", end: "11:00 am" },
{ date: "07/10/2020", start: "11:00 am", end: "12:00 am" },
{ date: "07/10/2020", start: "12:00 am", end: "01:00 pm" },
];

我想过滤上面的数组和相同的日期值。下面是这样的。

[
{
date: "06/10/2020",
count: 3,
slots: [
{ start: "10:00 am", end: "11:00 am" },
{ start: "11:00 am", end: "12:00 am" },
{ start: "12:00 am", end: "01:00 pm" },
],
},
{
date: "07/10/2020",
count: 3,
slots: [
{ start: "10:00 am", end: "11:00 am" },
{ start: "11:00 am", end: "12:00 am" },
{ start: "12:00 am", end: "01:00 pm" },
],
},
];

对于这种场景,您需要使用Object。因为我们无法将重复的值存储在Objects中。

所以答案是

data = [
{ date: "06/10/2020", start: "10:00 am", end: "11:00 am" },
{ date: "06/10/2020", start: "11:00 am", end: "12:00 am" },
{ date: "06/10/2020", start: "12:00 am", end: "01:00 pm" },
{ date: "07/10/2020", start: "10:00 am", end: "11:00 am" },
{ date: "07/10/2020", start: "11:00 am", end: "12:00 am" },
{ date: "07/10/2020", start: "12:00 am", end: "01:00 pm" },
];
let finalData = {};
for (const x of data) {
if (finalData[x.date]) { // if the key is already there increase the count and push the slots value
finalData[x.date].count += 1;
finalData[x.date].slots.push({ start: x.start, end: x.end });
} else { // otherwise set the new value
finalData[x.date] = { date: x.date, count: 1, slots: [{ start: x.start, end: x.end }] };
}
}
var value = Object.values(finalData);
console.log(JSON.stringify(value, null, 2));

首先,使用reduce将具有日期槽键值的对象分组。

然后,使用Object.entries将分组对象转换为键值对的数组

最后,map通过这些配对,并获取预期输出的相应值

const input = [
{ date: "06/10/2020", start: "10:00 am", end: "11:00 am" },
{ date: "06/10/2020", start: "11:00 am", end: "12:00 am" },
{ date: "06/10/2020", start: "12:00 am", end: "01:00 pm" },
{ date: "07/10/2020", start: "10:00 am", end: "11:00 am" },
{ date: "07/10/2020", start: "11:00 am", end: "12:00 am" },
{ date: "07/10/2020", start: "12:00 am", end: "01:00 pm" },
]
const res = Object.entries(
input.reduce((acc, { date, ...el }) => {
if (acc[date]) {
acc[date].push(el)
} else {
acc[date] = [el]
}
return acc
}, {})
).map(([key, value]) => ({
date: key,
count: value.length,
slots: value,
}))
console.log(res)
.as-console-wrapper { max-height: 100% !important; }

var arr = [
{ date: "06/10/2020", start: "10:00 am", end: "11:00 am" },
{ date: "06/10/2020", start: "11:00 am", end: "12:00 am" },
{ date: "06/10/2020", start: "12:00 am", end: "01:00 pm" },
{ date: "07/10/2020", start: "10:00 am", end: "11:00 am" },
{ date: "07/10/2020", start: "11:00 am", end: "12:00 am" },
{ date: "07/10/2020", start: "12:00 am", end: "01:00 pm" },
];
const result = [
...arr
.reduce((hash, { start, end, date }) => {
const current = hash.get(date) || { date, slots: [] };
current.slots.push({ start, end });
return hash.set(date, current);
}, new Map())
.values(),
];
result.map((x) => {
x.count = x.slots.length;
});
console.log(result);

const data = [
{ date: "06/10/2020", start: "10:00 am", end: "11:00 am" },
{ date: "06/10/2020", start: "11:00 am", end: "12:00 am" },
{ date: "06/10/2020", start: "12:00 am", end: "01:00 pm" },
{ date: "07/10/2020", start: "10:00 am", end: "11:00 am" },
{ date: "07/10/2020", start: "11:00 am", end: "12:00 am" },
{ date: "07/10/2020", start: "12:00 am", end: "01:00 pm" },
];

查找您的日期值

const dateArray = data.map((item) => item.date);
console.log(dateArray);

根据天数查找不同日期

const sortbydate = dateArray.map((item) => item.split("/")[0]);
console.log(sortbydate);

找到一个唯一的值来确定应该创建多少个阵列

const uniqueDates = [...new Set(sortbydate)];
console.log(uniqueDates);
const arr1 = [];
const arr2 = [];
function FindDateObje() {
for (let i = 0; i < sortbydate.length; i++) {
if (sortbydate[i] == uniqueDates[0]) {
arr1.push(data[i]);
} else {
arr2.push(data[i]);
}
}
return [arr1, arr2];
}
console.log(FindDateObje());

最新更新