-1

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?

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
user222137
  • 41
  • 6
  • Does this answer your question? [Find the min/max element of an array in JavaScript](https://stackoverflow.com/questions/1669190/find-the-min-max-element-of-an-array-in-javascript) (probably not) – freedomn-m Jun 11 '21 at 14:48
  • Your array is not valid JavaScript – mplungjan Jun 11 '21 at 14:50
  • The example doesn't seem to be JavaScript ..? – Teemu Jun 11 '21 at 14:50
  • 1
    Loop backwards through your array, at the first negative (which will be the last as going backward) record the index and `break` the loop - that's now the index of "last negative" - compare that with `i`. This assumes you want to know when it's *not* the last negative, otherwise you already have your last negative and don't need to loop again. – freedomn-m Jun 11 '21 at 14:50
  • Are you just trying to find out if the *last numeric value of the value array PER object in the myArray array* is negative? – LoveDev Jun 11 '21 at 14:50
  • Do you want to *find* the last negative (as in the -3 for (a) -4 for (b)) or do you want to check each value and do something if it's *not* the last negative? – freedomn-m Jun 11 '21 at 14:54

1 Answers1

1

Not entirely sure what you're trying to achieve as the question is hard to understand and your example code is...well I dunno what it is but it ain't valid JS.

Anyhow,

If you're looking to find out if the last value of the values array per object is negative. Do the below

for(var i = 0; i < myArray.length; i++) {
  var currObj = myArray[i];
  var lastValue = currObj.value[currObj.value.length - 1];
  var valIsNegative = parseInt(lastValue) < 0;

  if (valIsNegative) {

    // Last value is negative, do things.
  }

  if (!valIsNegative) {
    
    // Last Value is positive, do things.
  }
}

Again, not sure if this is even relevant to your question as it is hard to decipher. Let me know and I'll delete if it's not.

LoveDev
  • 257
  • 1
  • 9
  • ye, something like this, my bad to my bad question, the declaration of the array was a concept. Thanks for the answare – user222137 Jun 29 '21 at 14:41
  • No worries, in the future I recommend typing out your code within the question, many a time I've just had a "click" moment whilst doing so and just end up answering my own question. Also not sure how experienced you are but your question has nothing to do with jQuery. It's vanilla JS, jQuery is just a JS Library. – LoveDev Jun 30 '21 at 09:24