RN 中制作猜你喜欢

步骤一:产生一定数量的不重复的随机数

licked(data) {
const arr = []
//先放入一个随机数
arr[0] = Math.floor((Math.random() * data.length))
let count = 1
for (let i = 0; count < 5; i++) {
//产生5随机数
let flag = Math.floor((Math.random() * data.length))
if (this.panduan(flag, arr) === true) {
arr.push(flag)
count++
}

} return arr
}

panduan(data, arr) {
//判断有没有重复
for (let i = 0; i < arr.length; i++) {
if (arr[i] === data) {
return false
}

}
return true
}

步骤二:把产生的随机数作为index,,下标判断遍历数组

<ScrollView horizontal={true} showsHorizontalScrollIndicator={false} style={{ marginLeft: 18 }}>

{
this.licked(this.state.dataSour).map((ite, index) => {//产生随机数的数组
return (
this.state.dataSour.map((item, index) => {//原始数组
return (ite === index ?
<View style={{ width: 150, height: 150, marginRight: 18 }}>
<Image style={{ width: 150, height: 100 }} source={item.Imag} />
<Text style={{ paddingTop: 18, fontSize: 14, lineHeight: 18, color: '#333333' }}>{item.describe}</Text>
</View> : null
// console.log('sadfdgfhjghkjk')
)
})
)
})
}
</ScrollView>

注意count 的个数小于等于data.length否则会形成死循环

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值