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);
@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
user31050, I will take a look and as soon as I can update the reply.
– Samir Braga
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.
– Samir Braga
@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.
– user31050
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
– user31050