gpt4 book ai didi

reactjs - 如何破坏.map函数

转载 作者:行者123 更新时间:2023-12-03 13:21:25 24 4
gpt4 key购买 nike

如何破坏 .map 函数?下面附有代码示例。我想在索引达到 5 时中断外观,因为我只想在屏幕上渲染 5 个头像。

<View style={{ flexDirection: 'row', marginTop: 20, marginLeft: 30  }}>
{
peopleGroup.map((people, i) => {
if(i<5) {
return (
<Avatar
key={people.id}
width={30}
position='absolute'
containerStyle={{ transform: [{translate: [-28 + (28 * i), 0, 1 - (i * 0.1)]}] }}
small
rounded
source={{uri: people.image}}
activeOpacity={0.7}
/>
)
}else if(i===5) {
return (
<View key={i} style={{ transform: [{translate: [(25 * i), 9, 0]}] }}>
<Text>{peopleGroup.length}</Text>
</View>
)
}
}
)
}
</View>

最佳答案

使用Array.slice在绘制 map 之前。

peopleGroup
.slice(0, 5) // creates a copy of original, 5 items long
.map(...) // subsequent operations work on the copy

田田!

关于reactjs - 如何破坏.map函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48248067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com