0

In my Below Example Im trying to extract a variable from entries("Bookings") The user has made. If one was made on a easter holiday I set a cell on the DB with the date if its not set the value = "" e.g Easteryear = "2022" || Easteryear = "" on the db. But Im finding it difficult to extract the latest value that defined. without overwriting it the very next iteration of the loop

Example of the data im looking for is: Data Filled cells id:1 Easteryear ="2021" || id:2 Easteryear ="2022" || id:3 Easteryear ="" I want the 2022. But My Values keep getting set to Null/NaN

My approach to this is more then likely far from right but I need help extracting the latest value that is not empty in the loop (If I can do it outside a loop would be awesome, However I do need to get that Value so I can set another Variable for later Comparison)

Short Code TLDR: I Tried to Create a Check to see if there are no Values found(A.k.A Easteryear is empty on all entries) Create a Value for the user as if they are here they are booking a value on easter. Then find the latest and set userEasteryear

 if(this.personalEntries && this.personalEntries.length){
       
        this.personalEntries.forEach(az => {
          if(az.easterYear == null || az.easterYear == "" && easterindex <= 1){
            easterindex++;
            this.easterYear = year.toString();
            return; //Trying to break out oft he loop if this happens, Its a bad idea Thinking about it now.
          }else{
               var foundEasterYear = parseInt(az.easterYear);
                if(foundEasterYear !== NaN ){
                userEasteryear = foundEasterYear; //My last ittiration of the if I kept getting leaks 

              }
              az.inbet.forEach(i => {
              var x = (i.month+"-"+i.day+"-"+i.year);
              personalInbet.push(x);//This is a Temorary Array to Set the Dates Inbtw Start/End Dates
          });
          }
        });

PersonalEntries Structure:

  • Surname
  • Start Date
  • End Date
  • InBetween[] //Array of Dates From Start to End Date.
  • Easteryear
Someguy
  • 414
  • 3
  • 16
  • with input `id:1 Easteryear ="2021" / id:2 Easteryear ="2022" / id:3 Easteryear =""` you expect the latest year that is defined and not the empty one ? so basically, always the highest defined year ? – Crocsx Jan 14 '22 at 09:32
  • Yes Im trying to find a way to extract the latest year defined in the data. – Someguy Jan 14 '22 at 09:33

1 Answers1

2

So if I get it correctly, give a list of PersonalEntries you want to return the highest year that is specified. Then it is simply similar to

Finding the max value of an attribute in an array of objects

const data = [
  {id:1, Easteryear: "2021"},
  {id:2, Easteryear: "2022"}, 
  {id:3, Easteryear: ""}
]


function getHighestField(objArray, fieldName) {
  return Number(
    Math.max.apply(
      Math,
      objArray?.map(o => o[fieldName] || 0),
    ) || 0,
  );
}

console.log(getHighestField(data, 'Easteryear'));

if you want the full object

    const data = [
      {id:1, Easteryear: "2021"},
      {id:2, Easteryear: "2022"}, 
      {id:3, Easteryear: ""}
    ]


    function getHighestField(objArray, fieldName) {
      return objArray.reduce((a,b)=>a[fieldName]>b[fieldName]?a:b);
    }

    console.log(getHighestField(data, 'Easteryear'));
Crocsx
  • 2,534
  • 1
  • 28
  • 50
  • 1
    holy moly! This is awesome, I did not know this exists the amount of redundancy u just saved me from sir! I thank you. This is more then Enough to build off from! – Someguy Jan 14 '22 at 09:49