m9m9m
m9m9m

Reputation: 1721

how to create state array in react js

I'm creating a simple to-do app where the user can select the date, write the todo and set from and to time.

I have added 4 fields in my render function. Please guide me if my approach is wrong. at the end I want have an array todolist with index element and subarray index element as date and and children elements have todo, from and to time.

example:

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
    ]
]

for the above requirement how to initailaize the state?

Upvotes: 1

Views: 2032

Answers (2)

Achraf C.
Achraf C.

Reputation: 427

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}
       ]
    }
]

Try making objects of the items in the array

Upvotes: 0

Mehdi Aarab
Mehdi Aarab

Reputation: 23

I suggest that you store yours todos as objects in your todolist array like:

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',
  }
];

and to filter through the array by date you can use the filter method:

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

Upvotes: 1

Related Questions