vendredi 4 décembre 2020

How can I check the fields from an array result

I have an array response from API. I want to check all of the dates from array result if it is greater than {today's date} before proceeding to the else if condition.

Here's what I did

Sample array result:
array = [
{
id = 1
name = Jerry
date = 2020-07-17
subject = Math
},
{
id = 2
name = Tom
date = 2015-07-17
subject = Science
},
{
id = 3
name = Lily
date = 2018-07-17
subject = Biology
}
]
for (i = 0; i < array.length; i++){
 
   if (new Date(array[i].dates) > today) {
     //do something
   
   }else if (new Date(array[i].dates) < today) {
     //do something
 }else{
     //do something
  }
 }
}
  1. I notice that it is checking all the conditions. When the index becomes 1 it goes to if,elseif,else condition.
  2. So I figured out that it is not the right way to check the all of the dates in an array before proceeding to the next condition which is the else if condition.
  3. For example I have three result in my array and I want to check all of the dates from each index in the first condition and if it satisfy the condition then it will do its designated task in that condition.

Aucun commentaire:

Enregistrer un commentaire