Untitled
user_5113453
javascript
a year ago
1.7 kB
19
Indexable
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div>Hello World!</div>
<script>
const isSameDate = (dateStr1, dateStr2) => {
const date = new Date(dateStr1);
const d = date.getDate();
const m = date.getMonth();
const y = date.getFullYear();
const date2 = new Date(dateStr2);
const d2 = date2.getDate();
const m2 = date2.getMonth();
const y2 = date2.getFullYear();
return d === d2 && m === m2 && y === y2;
};
const data = [
{
id: 1,
kickOffTime: "2024-06-15T02:00:00",
title: "Việt Nam",
},
{ id: 2, kickOffTime: "2024-06-30T02:00:00", title: "Trung Quốc" },
{
id: 3,
kickOffTime: "2024-06-30T02:00:00",
title: "Hàn Quốc",
},
{
id: 4,
kickOffTime: "2024-06-15T02:00:00",
title: "Nhật Bản",
},
];
const dataFormatted = data.reduce((result, item) => {
const findDate = result.find((it) =>
isSameDate(it.date, item.kickOffTime)
);
if (findDate) {
findDate.data.push(item);
result = result.map((it) =>
isSameDate(it.date, findDate.date) ? findDate : it
);
} else {
result = [
...result,
{
date: item.kickOffTime,
data: [item],
},
];
}
return result;
}, []);
console.log(dataFormatted);
</script>
</body>
</html>
Editor is loading...
Leave a Comment