如何从对象数组中随机显示一个1图像

发布于 2025-02-13 05:44:08 字数 536 浏览 1 评论 0 原文

如何随机显示单个图像? 我创建了一个对象数组,现在添加了图像,我需要随机显示这些图像,但会一次显示这些图像,并且会在下一个重新加载中发生变化。

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>
   </>
  )
}

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>
   </>
  )
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

德意的啸 2025-02-20 05:44:08

使用此代码

function getRandomImage(arr) {
  const length = arr.length;
  const randomIndex = Math.floor(length * Math.random())
  return arr[randomIndex]
}

use this code

function getRandomImage(arr) {
  const length = arr.length;
  const randomIndex = Math.floor(length * Math.random())
  return arr[randomIndex]
}
标点 2025-02-20 05:44:08
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>
   </>
  )
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>
   </>
  )
黯然#的苍凉 2025-02-20 05:44:08

使用 MATH.FLOOR(),您可以在此链接,我希望这会有所帮助。谢谢

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>
   </>
  )
}

use Math.floor(), you can see the docs on this link, I hope this would be helpful. 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>
   </>
  )
}
相权↑美人 2025-02-20 05:44:08

一种方法是在图像数组的0和最后一个索引之间生成一个随机数。理想情况下,您将其存储在 usestate 中,以便在需要时可以更改它(例如,您的按钮说“获取随机GIF”)。

您将需要这样的函数来生成 RandomIdx

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

创建此类函数的方法在此处

然后,您将放入JSX和&lt; img/&gt; 标签,其 src 属性存储在对象的src属性中,at gifgallery [rancomidx]


  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>
        </>
    )
}

您在此反应游乐场

One way would be to generate a random number between 0 and the last index of your image array. 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").

You would need a function like this one to generate the 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].


  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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文