In my current coding situation, I am trying to organize an array based on the second element of each contained tuple. The process appears to be running smoothly until it reaches the last element. At that point, an exception is thrown.
ERROR TypeError: Cannot read property '1' of undefined
Below is the code I have been using:
public list:Array<[string, number]> = new Array<[string, number]>();
//{["A", 0],["B", 8], ...}
...
var sorted = false
while (!sorted){
sorted = true;
this.list.forEach(function (element, index, array){
alert(element);
if (element[1] > array[index+1][1] ) {
array[index][1] = array[index+1][1];
array[index+1] = element;
sorted = false;
}
});
}
I'm struggling to understand why this particular code isn't producing the desired results.