Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
Search in an array of objects and return a value from the last corresponding Object in JS

I'm struggling to return a value for last event containing "SearchResults" in the case below: schema of my datalayer where I want to collect the information

At the moment I achieved to write to following code in order to know if a SearchResult event exists or not:

    //Check if an existing event contains Search Results
if (digitalData.event.filter(e => e.componentID === 'SearchResults').length > 0) {    
    // If there are any that exist, I want to take the last one that was generated and return the pageIndex value of that one
    console.log("exist");  
    } else {
        // If it doesn't exist return 1
        return 1;
  };

Now I'm struggling to find a way to select only the last event generated and return the value contained in "digitalData.event.attributes.pageIndex".

Does anyone have any solution regarding this point ?

Thank you,

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could just save that in a variable temporarily and return the last result. Is this what you wanted?

const searchResults = digitalData.event.filter(e => e.componentID === 'SearchResults')
if (searchResults.length > 0) {    
    const yourAnswer = searchResults[searchResults.length -1]
    console.log("exist");  
    } else {
        
        return 1;
  };
about 4 years ago · Juan Pablo Isaza Denunciar

0

Filter the items first, and if there are any take the last item:

const searchResults = digitalData.event.filter(e => e.componentID === 'SearchResults');
if (searchResults.length > 0) {
  // If there are any that exist, I want to take the last one that was generated and return the pageIndex value of that one
  return searchResults[searchResults.length-1].pageLength;
} else {
  // If it doesn't exist return 1
  return 1;
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

Hello bro you can do what you want in this way.

let getResult = digitalData.event.filter(e => e.componentID === 'SearchResults' && e.componentID.length > 0 )
  
    if (getResult.length > 0) {
    const getIt = getResult[getResult.length - 1];
    getResult = getIt
    } 

    console.log(getResult)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda