Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

88
Vistas
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>
   </>
  )
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

use this code

function getRandomImage(arr) {
  const length = arr.length;
  const randomIndex = Math.floor(length * Math.random())
  return arr[randomIndex]
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

you can use random function to get the random index from your array

gifGallery[Math.random() * gifGallery.length].src
about 4 years ago · Juan Pablo Isaza Denunciar

0

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>
   </>
  )
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda