Having an Array of Arrays, my Array is named splitarr [Array2[], Array1[], Array0[], Array3[]...]. Unfortunately, it is not ordered correctly from Index Zero to index 2. Therefore, I am seeking a way to rearrange splitarr so that it follows this order => splitarr [Array0[], Array1[], Array2[], Array3[]...]. However, the code I have implemented does not seem to be working as expected. Whenever I attempt to console.log my Array, the indexes where the elements should be switched end up being undefined.
function blabla(){
dividersecond = 2;
splitarrayindex = 0;
splitarr = [[], [], [], []] //this line is just a placeholder, as I already have a functioning array
splitarr = ReorderArray(dividersecond, splitarrayindex, splitarr);
console.log(splitarr);
}
I have a function (which is just a portion of the entire function due to length constraints), in which I call the function responsible for reordering my array.
function ReorderArray(Count, Index, Array){
var originalIndex = Index;
for(Index; Index<Count; Index++){
var swapIndex = (Count-Index);
var temp = Array[Index];
Array[Index] = Array[swapIndex];
Array[swapIndex] = Array[temp];
}
return Array();
}
When executed as above, my console output is
[Array(8), undefined, undefined, Array(8), Array(8), Array(8)]
I have also attempted
...
Array[Index] = Array[swapIndex];
Array[swapIndex] = Array[temp];
return Array();
}
}
Yet, in this case, the console displays something like this.
[Array(8), Array(4), undefined, Array(8), Array(8), Array(8)]
Though I can speculate what might be causing the issue, I am unsure how to rectify it.
It goes without saying that returning my array within the loop makes no sense at all and will only terminate the loop. Nonetheless, it sheds light on what actually occurs during each iteration of the loop.
Despite extensive debugging, I have not been able to reach a definitive conclusion. Thus far, it appears that the value at Array[Index] becomes undefined at its designated position every time the loop is executed.
Hence, the initial loop looks like this
[Array(8), Array(4), undefined, Array(8), Array(8), Array(8)]
and the subsequent loop follows suit with
[Array(8), undefined, undefined, Array(8), Array(8), Array(8)]
Your assistance in resolving this matter would be greatly appreciated!