How to make array2 have the same ratio between indexes after array1 shuffle

Asked

Viewed 43 times

2

array1 = [1,2,3];
array2 = ["one","two","three"];

array1 and array2 have a direct relationship between the indices.

Shuffled array1 -> array1 = [3,1,2];

How to make array2 have the same relation between the indexes after the shuffle of array1, to be as for example:

array2 = ["three","one","two"]?

https://jsfiddle.net/y03d9kjw/

    array1 = [1,2,3];
    array2 = ["um","dois","três"];
    //Relação dos arrays entre os índices

    document.write(array1);
    document.write('<br />');
    document.write(array2);


    // embaralhou array1
    array1 = [3,1,2];
    document.write('<br /><br /><br />');
    document.write(array1);


    // Como ficar o array2 = ["três","um","dois"] a partir do embaralhamento do array1;
  • @Samir found a bug in the algorithm as you can see: array1 = [1,3,1]; array2 = ["UM", "three", "a"];// shuffled array1 array1 = [1, 1, 3]; output -> 1,1,3 a,um,three https://fiddle.jshell.net/r8n5wkkw/ is not working as it should, i would like to fix but I couldn’t even understand what was done in those foreach with [el] because I don’t know enough, if you can take a look again please grateful

  • user31050, I will take a look and as soon as I can update the reply.

  • user31050, I’m having some trouble getting this problem fixed in the short term, I see that the problem is based on the order of the repeated elements, since if the array1 = [1, 3, 1] is changed to [1, 1, 3], the possibilities for array2 are ["ONE", "one", "three"] and ["one", "one", "three"]. But I’ll keep trying later.

  • @Samirbraga This algorithm would be to sort the data, the html elements, I don’t know if it could help you, but in this my most recent post http://answall.com/questions/143532/alignment-left-com-detec%C3%A7%C3%A3o-de-colis%C3%A3o-de-elements have been trying to align elements to the left, using as reference the sequence generated by the ordered array.

  • So to 'realign' the data, in the other arrays, after ordering, was using the function you made. In fact, I just needed to align the items left with collision detection, and the logic of the algorithm I thought was to reorder the data by the array, but maybe you have a better solution for the left alignment, could you take a look at the new post? I am very grateful because I know you are a good person. Thank you

1 answer

1


See if that’s the way you wanted it:

First I created an object in which the keys run to the first array and the respective values correspond to the second array:

var corresp = {};
array1.forEach(function(el, i) {
  corresp[el] = array2[i];
});

I’d take something like that

{
    "1": "um",
    "2": "dois",
    "3": "três"
}

And after shuffling the array1, just redo the second array from the object

// embaralhou array1
array1 = [3, 1, 2];

array1.forEach(function(el, i){
  array2[i] = corresp[el];
})

Upshot:

array1 = [1, 2, 3];
array2 = ["um", "dois", "três"];
//Relação dos arrays entre os índices
var corresp = {};
array1.forEach(function(el, i) {
  corresp[el] = array2[i];
})
document.body.innerHTML += (array1);
document.body.innerHTML += ('<br />');
document.body.innerHTML += (array2);

// embaralhou array1
array1 = [3, 1, 2];

array1.forEach(function(el, i) {
  array2[i] = corresp[el];
})
document.body.innerHTML += ('<br /><br /><br />');
document.body.innerHTML += (array1);
document.body.innerHTML += ('<br />');
document.body.innerHTML += (array2);

If you prefer a function:

 function match(array1, array1Sorted, array2) {
   var match = {};
   array1.forEach(function(el, i) {
     match[el] = array2[i];
   })
   array1Sorted.forEach(function(el, i) {
     array2[i] = match[el];
   })
   return array2;
 }

 var ar1 = [1, 2, 3, 4, 5, 6, 7];
 var ar2 = ['um', 'dois', 'três', 'quatro', 'cinco', 'seis', 'sete'];
 var ar1sorted = [7, 3, 4, 2, 1, 5, 6];


document.body.innerHTML += ar1sorted;
document.body.innerHTML += "<br>";
document.body.innerHTML += match(ar1, ar1sorted, ar2);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.