我有这样的代码:
return Object.keys(items).filter((item, index) => {
if(index <= this.state.limit)
return item
if(index > this.state.limit)
break? continue to the maping? how?
}).map( (item, index) => {
return(
<div key={index}>
{item.title}
</div>
)
})
我怎样才能退出过滤器并转到映射而不是循环遍历所有项目。是否可以?我试过中断并继续但没有成功。
object keys 中的数组(items)是一个带有一堆 props 的对象,大约 500 万)
const items = {
title2: 'bird',
title3: 'apple,
title4 ....
}
我基本上需要根据 this.state.limit
返回对象中 前 N Prop 的数量最佳答案
你可以使用 slice
代替 filter
来限制数组长度
return Object.values(items)
.slice(0, this.state.limit + 1)
.map( (item, index) => {
return (
<div key={index}>
{item.title}
</div>
)
})
但是您应该记住,不能保证对象中属性的顺序。
关于javascript - 停止 filter(),这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46034388/