Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to properly extract a defined value from an array in a loop (Without Resetting the Variable constantly)

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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'));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading