简体   繁体   English

如何从 object 的数组中仅随机显示一张 1 图像

[英]how to randomly display only one 1 image from the array of object

how to display single image randomly?如何随机显示单个图像? I have created an array of objects where the images are been added now I need to display those images randomly but one at once and it will change in the next reload.我创建了一个对象数组,其中添加了图像,现在我需要随机显示这些图像,但一次显示一个,并且它会在下一次重新加载时发生变化。

 const index= () => { let gifGallery=[ { src:'./Images/1.gif', value:1 }, { src:'./Images/2.gif', value:2 }, { src:'./Images/3.gif', value:3 }, { src:'./Images/4.gif', value:4 }, ] return ( <> <h1>Gif Gallery</h1> <div> { what should be the logic here????? } </div> </> ) }

use this code使用此代码

function getRandomImage(arr) { const length = arr.length; const randomIndex = Math.floor(length * Math.random()) return arr[randomIndex] }
You can get a random value from an array of objects and use it in your return method. let gifGallery=[ { src:'./Images/1.gif', value:1 }, { src:'./Images/2.gif', value:2 }, { src:'./Images/3.gif', value:3 }, { src:'./Images/4.gif', value:4 }, ] var randomObject = gifGallery[Math.floor(Math.random() * gifGallery.length)]; return ( <> <h1>Gif Gallery</h1> <div> <image src={randomObject.src} alt={randomObject.value} /> </div> </> )

use Math.floor() , you can see the docs on this link , I hope this would be helpful.使用Math.floor() ,您可以在此链接上查看文档,希望对您有所帮助。 thanks谢谢

 let gifGallery=[ { src:'./Images/1.gif', value:1 }, { src:'./Images/2.gif', value:2 }, { src:'./Images/3.gif', value:3 }, { src:'./Images/4.gif', value:4 }, ] const randomImage = Math.floor(Math.random() * gifGallery.length) console.log(randomImage)

 const index= () => { let gifGallery=[ { src:'./Images/1.gif', value:1 }, { src:'./Images/2.gif', value:2 }, { src:'./Images/3.gif', value:3 }, { src:'./Images/4.gif', value:4 }, ] const randomImage = Math.floor(Math.random() * gifGallery.length) return ( <> <h1>Gif Gallery</h1> <div> <img src={gifGallery[randomImage].src} /> </div> </> ) }

One way would be to generate a random number between 0 and the last index of your image array.一种方法是在 0 和图像数组的最后一个索引之间生成一个随机数。 Ideally you'd store it in a useState , so that you can change it when you want (say for instance you have a button that says "get random GIF").理想情况下,您会将其存储在useState中,以便您可以在需要时更改它(例如,您有一个显示“获取随机 GIF”的按钮)。

You would need a function like this one to generate the randomIdx :您需要像这样的 function 来生成randomIdx

 const getRandomIdx = () => Math.floor(Math.random() * gifGallery.length)

The way to create such functions is well explained in here .创建此类函数的方法在此处进行了很好的解释。

You would then put in your JSX an <img /> tag, whose src attribute is stored in the src property of the object at gifGallery[randomIdx] .然后,您将在 JSX 中放入一个<img />标记,其src属性存储在gifGallery[randomIdx]的 object 的 src 属性中。

 const gifGallery=[ { src:'./Images/1.gif', value:1 }, { src:'./Images/2.gif', value:2 }, { src:'./Images/3.gif', value:3 }, { src:'./Images/4.gif', value:4 }, ] const getRandomIdx = () => Math.floor(Math.random() * gifGallery.length) const App = () => { const [randomIdx, setRandomIdx] = useState(getRandomIdx()) return ( <> <h1>Gif Gallery</h1> <div> <img src={gifGallery[randomIdx].src} /> </div> <p>Idx: {randomIdx}</p> <button onClick={() => setRandomIdx(getRandomIdx())}> get another random one </button> </> ) }

you have a working exemple in this React Playground你在这个 React Playground中有一个工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM