Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!