0

This is not removing negative integers from an array. I can't figure out why not...

for(var ii = 0; ii < diffs.length; ii++) {
     if(Number(diffs[ii]) < 0) {
       diffs.splice(ii, 1);
     }
   }
2
  • Are these numbers in string format? Try converting them to Number: Number(diffs[ii]) < 0 Commented Aug 5, 2012 at 0:11
  • 1
    How are these "negatives" declared? Are you sure they are numbers and not strings? Commented Aug 5, 2012 at 0:11

4 Answers 4

2

You have two problems: 1) When you remove items in the array, you have to make sure you don't increment the counter (this can be avoided by counting from the end). 2) You have to store the result from splice and then add it to the removed array

for(var ii = diffs.length - 1; ii >= 0; ii--) {
    if(+diffs[ii] < 0) { // use the unary plus operator to convert to a number, just in case
        removed.push(diffs.splice(ii, 1)[0]); //splice returns an array - get the first and only element in it
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can't traverse the array upwards from 0 when you are modifying the array during the traversal because removing the current element will move the other elements down in the array and cause you to skip the next element.

A common way to solve this is to traverse the array elements in reverse order because the elements that are repositioned by removing the current element are ones that you have already traversed, not the ones the for loop is still going to process next:

for(var ii = diffs.length - 1; ii >= 0; ii--) {
    if(Number(diffs[ii]) < 0) {
        removed.push(diffs[ii]);
        diffs.splice(ii, 1);
    }
}

You also have to push the removed item BEFORE you remove it from the array.

Comments

0

The index can be fixed also like this:

for (ii = 0; ii < diffs.length; ii++) {
    if (Number(diffs[ii]) < 0) {
        removed.push(diffs[ii]);
        diffs.splice(ii, 1);
        ii--;
    }
}
console.log(diffs);
console.log(removed);

fiddle

As now I see, posted this answer too late again :)

Comments

0

When removing item while traversing an array forward, you will need to adjust the loop variable or you skip the next element. The other possibility is to traverse backwards.

for (var i=0; i<diffs.length; i++)
    if (Number(diffs[i]) < 0)
        diffs.splice(i--, 1);

// OR

for (var i=diffs.length-1; i>=0; i--)
    if (Number(diffs[i]) < 0)
        diffs.splice(i, 1);

It might be easier (and maybe even faster) to create a new array and overwrite the diffs variable with it. This works very elegant with the filter() method:

var diffs = diffs.filter(function(diff) { return Number(diff) >= 0; });

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.