vendredi 4 janvier 2019

Randomly selecting elements in an array only once each while keeping track of their original position - JavaScript

I am currently coding in JavaScript for a project where I need to create modified arrays based on an original one. I am having an issue when it comes to randomly selecting the elements of the original array that need to be modified. For the project's purposes, I need to keep track of the original position of each elements, but I also cannot pick the same element twice.

So far, I had been randomly selecting an item using this:

var originalArray[1, 2, 4, 3, 5, 2, 5];

var modifiedArray[];

var copyOriginalArray = originalArray;

var score = Math.floor(Math.random() * copyUser.length);

var targetScore = copyUser[score];

targetProfile.push(targetScore);

copyUser.splice(score, 1);

But the issue is that, while it does randomly select an item and ensures that it will not be selected again, it does not allow me to keep track of the original position of the items. Also, since some of the items may be the same, I can't simply trace them back to the original array.

What do you suggest doing?

Thank you very much!




Aucun commentaire:

Enregistrer un commentaire