我想渲染图像的数量和图像应该在反应原生中随机播放? 怎么做?谁能帮我



当我在 react native 中单击该特定图像时,图像应该被洗牌。

您可以使用 lodash 库的 shuffle,也可以使 shuffle 函数化。 在下面的示例中,我将图像 URL 作为数字。

function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// Used like so
var arr = [2, 11, 37, 42];
arr = shuffle(arr);
console.log(arr);

参考:如何随机化(洗牌(一个 JavaScript 数组?

最新更新