From 5dde57683d15e203ca48f5a3ece2a95b12ef651f Mon Sep 17 00:00:00 2001 From: Ken Date: Wed, 13 Feb 2019 13:32:57 -0800 Subject: [PATCH] cleaner filtertodos --- playground/src/components/TodoList.tsx | 33 +++++--------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/playground/src/components/TodoList.tsx b/playground/src/components/TodoList.tsx index 3b86413..73cb6f2 100644 --- a/playground/src/components/TodoList.tsx +++ b/playground/src/components/TodoList.tsx @@ -18,36 +18,15 @@ type TodoListProps = ReturnType & ReturnType { render() { const { filter, todos } = this.props; - let filteredTodos: typeof todos = {}; - - switch (filter) { - case 'completed': - Object.keys(todos).forEach(id => { - if (todos[id].completed) { - filteredTodos[id] = todos[id]; - } - }); - break; - - case 'active': - Object.keys(todos).forEach(id => { - if (!todos[id].completed) { - filteredTodos[id] = todos[id]; - } - }); - break; - - default: - filteredTodos = todos; - break; - } + const filteredTodos = Object.keys(todos).filter(id => { + return filter === 'all' || (filter === 'completed' && todos[id].completed) || (filter === 'active' && !todos[id].completed); + }); return ( - {Object.keys(filteredTodos).map(id => { - const todo = filteredTodos[id]; - return ; - })} + {filteredTodos.map(id => ( + + ))} ); }