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

187
Vistas
Get index of shuffled blob [blob of an image , audio etc...]

I have an object containing array of blobs (represented as strings here!) and a pictureIndex like this:

const data = {
    pictures: ['blob1','blob2','blob3'],
    pictureIndex: 0 
}

shuffleArray(data.pictures);

Note: pictureIndex is the index of 'blob1'

I want to shuffle the blobs inside pictures array to have a new random arrangement of the blobs but I want to get the index of blob1 at the final shuffled array. So if shuffled data is this:

 ['blob2','blob3','blob1']

I want to return 2 as pictureIndex of 'blob1'...

So far I can shuffle the pictures easily like this without any idea how to modify the pictureIndex:

const data = {
    pictures: ['blob1','blob2','blob3'],
    pictureIndex: 0 
}


shuffleArray(data.pictures);

function shuffleArray(array) {
    let currentIndex = array.length,  randomIndex;
    while (0 !== currentIndex) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex--;
        [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
    }
    return array;
}

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

One way would be to make the shuffle function, accept the full object, then return the full object with the shuffled array and updated index.

It would then save you from needing to find the index again from the shuffled array.

let data = {
  pictures: ['blob1', 'blob2', 'blob3'],
  pictureIndex: 0
}

data = shufflePictures(data);

console.log(data)

function shufflePictures(data) {
  let currentIndex = data.pictures.length,
    randomIndex;
  while (0 !== currentIndex) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex--;
    [data.pictures[currentIndex], data.pictures[randomIndex]] = [data.pictures[randomIndex], data.pictures[currentIndex]];
    if (randomIndex === data.pictureIndex) {
      data.pictureIndex = currentIndex
    }
  }
  return data
}

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