在 JSX 中,我正在映射一個月內計算的天數:
{range(daysInMonth).map((i) => (
<div
className="day-cell day-cell--in-month"
key={i}
>
{i 1}
</div>
))}
然后我有一系列來自 api 的事件:
const events = [
{
date: timestamp,
description: "Meeting with friends"
}
//More events//
]
如何映射事件陣列并將事件時間戳與正在映射的 daysInMonth 日期的當前時間戳匹配,然后顯示事件描述?
uj5u.com熱心網友回復:
如果上述評論的假設...
“daysInMonth 只是一個數字,例如二月是 28。” ...因此,可以假設事件項的時間戳/日期值也是整數嗎?
filter... 適用,可以與 . 合作實施上述任務some。
const events = [{
date: timestamp,
description: "Meeting with friends"
}/*, { More events }*/];
{
range(
daysInMonth
).filter(dayValue =>
events.some(({ date }) => date === dayValue)
).map(dayValue => (
<div
className="day-cell day-cell--in-month"
key={dayValue}
>
{dayValue 1}
</div>
))
}
編輯
“......因此,可以假設一個
events專案的時間戳/日期值也是一個整數?” ——彼得·塞利格“......這是來自 MySQL 資料庫的時間戳。” – 黏附劑
“......然后在我的答案已經提供的過濾器代碼中,必須從每個專案的值中獲取
day,以使其與每個專案的整數值相當。” ——彼得·塞利格eventsdatedaysInMonth
const events = [{
date: timestamp,
description: "Meeting with friends"
}/*, { More events }*/];
{
range(
daysInMonth
).filter(dayValue =>
events.some(({ date }) =>
new Date(date).getDay() === dayValue
)
).map(dayValue => (
<div
className="day-cell day-cell--in-month"
key={dayValue}
>
{dayValue 1}
</div>
))
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/431166.html
標籤:javascript 数组 日期 筛选 时间戳
