Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

397
Visualizações
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)

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
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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'));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda