[英]Same property of switch case JS
I'm using React and I don't want to reply <ListItem ...> code as many times as the cases are.我正在使用 React 并且我不想像案例一样多次回复 <ListItem ...> 代码。 There is a method to simplify and reduce this code?有没有一种方法可以简化和减少这段代码?
displayToDoList = () => {
switch (this.state.sorted) {
case 0:
return (this.state.todosById.map(todo =>
<ListItem
key={todo.id}
plainText={todo.plainText}
name={todo.name}/>))
break;
case 1:
return (this.state.todosByName.map(todo =>
<ListItem
key={todo.id}
plainText={todo.plainText}
name={todo.name}/>))
break;
case 2:
return (this.state.todosByCreatedAt.map(todo =>
<ListItem
key={todo.id}
plainText={todo.plainText}
name={todo.name}/>))
break;
case 3:
return (this.state.todosByCharcacters.map(todo =>
<ListItem
key={todo.id}
plainText={todo.plainText}
name={todo.name}/>))
break;
}
}
I think that there will be a better way than mine.我认为会有比我更好的方法。
instead of case
0, 1, 2.. make this.state.sorted
= 'todosById','todosByName',..而不是case
0, 1, 2.. make this.state.sorted
= 'todosById','todosByName',..
then just return然后就回来
displayToDoList = () => {
return (this.state[this.state.sorted].map(todo =>
<ListItem
key={todo.id}
plainText={todo.plainText}
name={todo.name}/>))
}
const itemRenderer = todo =>
<ListItem
key={todo.id}
plainText={todo.plainText}
name={todo.name} />
switch (this.state.sorted) {
case 0:
return (this.state.todosById.map(itemRenderer)
case 1:
return (this.state.todosByName.map(itemRenderer)
case 2:
return (this.state.todosByCreatedAt.map(itemRenderer)
case 3:
return (this.state.todosByCharcacters.map(itemRenderer)
}
Ciao, I'm thinking something like: Ciao,我在想:
displayToDoList = () => {
let arrayToUse;
switch (this.state.sorted) {
case 0:
arrayToUse = this.state.todosById;
break;
case 1:
arrayToUse = this.state.todosByName;
break;
case 2:
arrayToUse = this.state.todosByCreatedAt;
break;
case 3:
arrayToUse = this.state.todosByCharcacters;
break;
}
return (arrayToUse.map(todo =>
<ListItem
key={todo.id}
plainText={todo.plainText}
name={todo.name}/>))
}
I would suggest more cleaner approach with object map lookup appraoch.我建议使用对象映射查找方法更简洁的方法。
displayToDoList = () => {
const sortedMap = {
0: this.state.todosById,
1: this.state.todosByName,
2: this.state.todosByCreatedAt,
3: this.state.todosByCharcacters,
}
const todoArr = sortedMap[this.state.sorted] || [];
return todoArr.map(todo =>
<ListItem
key={todo.id}
plainText={todo.plainText}
name={todo.name} />);
}
The lookup time is O(1) here, hence i would prefer it over the array representation provided by domenikk
查找时间在这里是 O(1),因此我更喜欢它而不是由domenikk
提供的数组表示
It can be simplified like this:可以这样简化:
displayToDoList = () => {
const types = ['todosById', 'todosByName', 'todosByCreatedAt', 'todosByCharcacters'];
const todos = this.state[types[this.state.sorted]];
return todos.map(todo =>
<ListItem
key={todo.id}
plainText={todo.plainText}
name={todo.name}/>
);
}
displayToDoList = () => {
// key could be id, date, name etc
const todos = sortTodosBy(todos, key)
return todos.map(todo =>
<ListItem
key={todo.id}
plainText={todo.plainText}
name={todo.name}/>
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.