从 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>
)
}
}
您需要做的就是在渲染对象时对它们进行排序:
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>
)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句