【发布时间】:2020-05-11 00:15:30
【问题描述】:
我想按月列出计划,但我的代码不起作用。我想设置一个标题,例如九月,然后列出该月的所有计划。
我的数据:
const plans = [
{
"done": true,
"datetime": "2020-01-22T01:00:00+00:00",
},
{
"done": true,
"datetime": "2020-01-25T03:00:00+00:00",
"name": "Read a book",
},
{
"done": false,
"datetime": "2020-01-29T01:00:00+00:00",
"name": "Travel",
},
{
"done": false,
"datetime": "2020-02-02T02:00:00+00:00",
"name": "Vaccations",
},
{
"done": false,
"datetime": "2020-03-02T01:00:00+00:00",
"name": "No plans",
},
{
"done": false,
"datetime": "2020-04-02T01:00:00+00:00",
"name": "Do excersice",
},
{
"done": false,
"datetime": "2020-05-04T01:00:00+00:00",
"name": "Work",
},
{
"done": false,
"datetime": "2020-05-04T01:00:00+00:00",
"name": "Watch netflix",
},
{
"done": false,
"datetime": "2020-05-04T01:00:00+00:00",
"name": "No plans",
}
]
这是在我的 App.js (React) 中:
return (
<div className="App">
{
plans.map((todo, idx) => {
const currentMonth = moment(todo.datetime).format('MM')
const anotherMonth = plans[idx + 1] ? plans[idx + 1].datetime : undefined
const anotherMonthConverted = moment(anotherMonth).format('MM')
console.log(anotherMonthConverted)
return(
<div key={idx}>
<div>
{
currentMonth <= anotherMonth ? (
<div>
<div>{moment(currentMonth).format('MMM')}</div>
</div>
):(
<div>
</div>
)
}
<div >{todo.name}</div>
</div>
</div>
)
})
}
</div>
);
}
【问题讨论】:
标签: javascript arrays json reactjs filter