Jquery check if is last negative number in an object array

  arrays, javascript

I need to check if in an object array the current value is the last negative.
For example:

var myArray = [
name:'a' value:'5','-1','-5','7','-3'
name:'b' value:'7','-5','-4','7','5'
]

In a for loop, if current value is negative, i need check if is the last negative value in the array for this name.

For example In name ‘b’:

current value = -5, it’s negative, so, Is the last negative? No

current value = -4, it is negative, so, Is the last negative? Yes

Start code for the example:

lastName = '';
for(i = 0; i < myArray.length; i++){
  if(myArray[i].value < 0 && lastName == myArray[i].name){
    // then it's negative, now check if is the last negative value
  }

lastName = myArray[i].name;
}

Inside the if I dont understand how to do this, can someone help me?

Source: Ask Javascript Questions

LEAVE A COMMENT