从Angular 进行orderBy过滤的React方法是什么 ?
在此示例中,您如何按年龄订购动物?
class Application extends React.Component {
constructor(props){
super(props);
this.state={
animals: [
{id: 1, age: 5, type: "cat"},
{id: 2, age: 3, type: "dog"},
{id: 3, age: 10, type: "wolf"}
]
}
}
render() {
let {animals} = this.state;
return (
<div>
{animals.map((animal)=>{
return (<p key={animal.id}>{animal.type}</p>)
})}
</div>
)
}
}
Run Code Online (Sandbox Code Playgroud)
您需要做的就是在渲染对象时对对象进行排序:
render() {
let {animals} = this.state;
return (
<div>
{[...animals].sort((a,b) => {return a.age - b.age}).map((animal)=>{
return (<p key={animal.id}>{animal.type}</p>)
})}
</div>
)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
740 次 |
| 最近记录: |