var arr = [
[ 1234, 3456 ],
[ 1324, 2345 ],
[ 6355 ],
[ 2234 ],
[ 8345 ],
[ 1234, 6355 ]
];
var i = arr.length;
while(i--){
var current = arr[i];
if(i == 0){
break;
}
var before = arr[i-1];
if(!before[1]) continue;
var result = current[1] - before[1]
console.log(result)
}
In the code above the child arrays which have value only in their 0 index will get eliminated as if(!before[1]) continue;.If you run this code result is NaN and then -1111 where NaN is because arr[2][1] is undefined while before is 2345. And -1111 is because arr[1][1] is 2345 and arr[0][1] is 3456 so substractiong before from current gives -1111 .
As you can see array in the last index of arr bypassed while it has value in its 1st index [1234, 6355]. But in this case current could be aqual to arr[5][1] at first and then before equal to arr[1][1] first of all. I want exactly the same thing, to bypass arrays when they don't have any value in index zero only for before variable, while current keeps the value until it finds before.
Shortly in this example result should be:
6355 - 2345 = 4010 //arr[5][1] - arr[1][1]
2345 - 3456 = -1111 //arr[1][1] - arr[0][1]
But currently it is only
NaN //arr[2][1] - arr[1][1]
-1111 /arr[1][1] - arr[0][1]
It should not eliminate any current when before is not found or any before when current is not found