如何在React JS中创建状态数组

时间:2018-06-27 09:10:57

标签: arrays reactjs setstate

我正在创建一个简单的待办事项应用程序,用户可以在其中选择日期,编写待办事项并进行时间设置。

我在渲染函数中添加了4个字段。如果我的方法有误,请指导我。最后,我想要一个数组todolist,其中index元素和subarray index元素作为日期,子元素必须在时间上和时间上待做。

示例:

todolist[
    27-06-2018 : [
       todo: eat breakfast
       from: 9:00
       to: 9:30
    ]
    28-06-2018 :[
       todo: eat lunch
       from: 12:00
       to: 12:30
    ]
]

对于上述要求,如何建立国家?

2 个答案:

答案 0 :(得分:1)

我建议您将待办事项作为对象存储在待办事项列表数组中,例如:

todolist[
  {
    date: '27-06-2018',
    todo: 'eat beakfast',
    from: '9:00',
    to: '9:30',
  },
  {
    date: '28-06-2018',
    todo: 'eat lunch',
    from: '9:00',
    to: '9:30',
  }
];

,并按日期过滤整个数组,可以使用过滤方法:

todoslist.filter(todo => todo.date == '27-06-2018');

答案 1 :(得分:0)

todolist[
{
    27-06-2018 : [
      { todo: eat breakfast},
       {from: 9:00},
      { to: 9:30}
     ]
    },{
    28-06-2018 :[
       {todo: eat lunch},
      { from: 12:00},
      { to: 12:30}
       ]
    }
]

尝试使数组中的项目成为对象