1

As my question states how would I determine the index of the last item in JavaScript array with a value > 0

So if myarray = [12, 35, 56, 0, 42, 0]

How would I find the index of the last positive integer in this array? i.e in this instance the answer would be 4 (no. 42).

1
  • 3
    Just loop through and check each number Commented Dec 7, 2012 at 15:43

6 Answers 6

5

There's no shortcut, just loop backward through the array from length - 1 to 0 (inclusive) and stop at the first >0 entry.

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

2 Comments

Really... why did I add an answer with some code. This answer is just the right one.
Thanks T.J.Crowder, this is what I shall do.
0

this should do it:

var myarray = [12, 35, 56, 0, 42, 0];
var found = {};
found.value = 0;
found.index = 0;
var j = myarray.length;
for (j; j>0; j--) {
    if (myarray[j] > 0) {
        found.value = myarray[j];
        found.index = j;
        break;
    }
}
alert(found.index);

EDIT: note the above would not clarify the 0 index or none so:

found.value = undefined;
found.index = undefined;
var j = myarray.length;
for (j; j>0; j--) {
    if (myarray[j] > 0) {
        found.value = myarray[j];
        found.index = j;
        break;
    }
}
alert(found.index);

Comments

0

Try this:

var array = [12, 35, 56, 0, 42, 0];
function getLastNotZero(array){
    var t = 0, i = array.length-1;
    while(t <= 0 && i >= 0){
        t = array[--i];
    }
    return i-1;
}
getLastNotZero(array);
//Returns 4

4 Comments

It needs to find a value >0 not !0
I see you've fixed the = vs. ==, which is good. Mind you, this is still an infinite loop. Edit Ah, now you've fixed that too.
There, the single = was a typo, seemingly cost me this answer :/. This wasn't a infinite loop, though.
The version in place when I commented had no check on i. I suppose the loop would have broken eventually, as undefined <= 0 is false.
0

Hope this helps:

for(var i=myarray.length-1; i>=0;i--){
    if(myarray[i] > 0){
       alert(i);
       break;
  }
}
if(i<0)
alert(i);

3 Comments

You need to go backwards through your array, not forwards.
@LeeTaylor: Technically, the above works (because he doesn't break the loop until the end). It just works a lot harder than necessary.
@T.J.Crowder - This is true.
0

Try this solution:

var myarray = [12, 35, 56, 0, 42, 0], index;
$.each(myarray,function(idx,el){
    if(el > 0){ 
        index = idx;
    }
});
alert('Index:'+index+', value:'+myarray[index]);

Comments

0

You can get the reverse index with .reverse() and .findIndex, then do some basic arithmetic to flip it:

> myarray = [12, 35, 56, 0, 42, 0]
> var ridx = myarray.slice().reverse().findIndex(n => 0 < n)
1
> var idx = myarray.length - 1 - ridx
4

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.