步骤一:产生一定数量的不重复的随机数
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否则会形成死循环
2555

被折叠的 条评论
为什么被折叠?



