0
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

9
  • I don't think you want to asign the variables in every loop.... Commented Nov 26, 2015 at 16:20
  • Array.prototype.map or Array.prototype.forEach would be better to avoid it (depends on what you want to do) Commented Nov 26, 2015 at 16:22
  • Could you describe what do you want to achive ? Commented Nov 26, 2015 at 16:30
  • nAz I gave an example of what I want Commented Nov 26, 2015 at 16:31
  • @developer in the example above you describe your logic but not what it should do Commented Nov 26, 2015 at 16:33

2 Answers 2

1

If I've understood your question correctly, you're trying to do decrementation with each sequential arr[n][1] excluding those arr[n]s, which have only one member.

You can do it this way:

var arr = [
        [1234, 3456],
        [1324, 2345],
        [6355],
        [2234],
        [8345],
        [1234, 6355]
    ],
    i = arr.length,
    result, firstOperand;

while (i--) {
    if (typeof firstOperand === 'undefined') { // No first operand 
        firstOperand = arr[i][1]; // Set the first operand
        continue;
    }
    if (arr[i].length < 2) {continue;} // No second operand, continue   
    result = firstOperand - arr[i][1];
    firstOperand = arr[i][1]; // Set a new first operand
    console.log(result);
}

A live demo at jsFiddle.

The trick is, that you store the first operand to a variable. Then check if it has a value. If not, assign a one and continue the loop. Then check if you've the second operand for decremention at the current index. If not found, continue the loop. Then when you've both operands, do the math, and assign a new firstOperand.

Notice, that typeof checking allows you to use also zero as a value. The code works also in a case when the length of some of the last members in arr is 1, or even when there are not valid members at all.

Sign up to request clarification or add additional context in comments.

Comments

0

[Update] I think what you want is something like this:

var arr = [
[ 1234, 3456 ],
[ 1324, 2345 ],
[ 6355 ],
[ 2234 ],
[ 8345 ],
[ 1234, 6355 ]
];
var result; // Prepare result var
var before = 0; // Prepare "before" var
var current = 0; // Prepare "current" var
for (var i=0; i<arr.length;i++){ // Loop the length of Arr
    before = current; // store previous "current" as "before"
    if (arr[i][1] != null){ // Skip if no entry in array
        current = arr[i][1]; // Set "current" to array value
    }
    result = current-before; // Update result at end of each loop
}
console.log(result);

This gives you the result you're looking for (4010), and will skip the arrays where the value is not set..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.