In JavaScript, I, using the common for loop iteration of array with variable i , I'm trying to create an array with sub-arrangements having data from other arrangements from an order relative to the position of i .
This code example describes the situation:
var arranjoBase1 = ["a", "b", "c", "d", "e"]
var arranjoBase2 = ["f", "g", "h", "i", "j"]
var arranjoBase3 = [1, 2, 3, 4, 5]
var arranjoFinal = [ [], [], [], [], [] ]
for(i = 0; i < arranjoFinal.length; i++)
{ arranjoFinal[i].push(arranjoBase1[i], arranjoBase2[i], arranjoBase3[i]);
if(i != 0) { arranjoFinal[i].push(arranjoBase3[i - 1]) }
else { arranjoFinal[i].push(arranjoBase3[arranjoBase3.length - 1]) };
if(i != arranjoBase3.length - 1) { arranjoFinal[i].push(arranjoBase3[i + 1]) }
else { arranjoFinal[i].push(arranjoBase3[0]) };
if(i == 0) { arranjoFinal[i].push(arranjoBase3[arranjoBase3.length - 2]) }
else if(i == 1) { arranjoFinal[i].push(arranjoBase3[arranjoBase3.length - 1 ]) }
else { arranjoFinal[i].push(arranjoBase3[i - 2]) };
if(i == arranjoBase3.length - 1) { arranjoFinal[i].push(arranjoBase3[1]) }
else if(i == arranjoBase3.length - 2) { arranjoFinal[i].push(arranjoBase3[0]) }
else { arranjoFinal[i].push(arranjoBase3[i + 2]) } };
//Resultado Final
var arranjoFinal = (5) [Array(7), Array(7), Array(7), Array(7), Array(7)]
//Exemplo de Arranjo Final
var arranjoFinal[0] = (7) ["a", "f", 1, 5, 2, 4, 3]
This code already gives me the results you want.
The point is that since I needed to use many if conditionals to get around the problem of whether the result of one of the arithmetic operations to find the next element to be added in the End array is out of the array indexBase3 - in which case it would be returned undefined instead of the desired element - I feel that there must be a much more practical solution to this result, which, for example, is also sustainable for larger arrays than five elements.
So, would there really be a more [practical / concise] way of achieving results like these? As, for example, and preferably, a way of saying that if the result of an arithmetic operation to find the index of the element of an array exceeds the index of that array this means which is for it to continue the operation from its [beginning / end]?