Tengo dificultades para devolver un valor para el último evento que contiene "Resultados de búsqueda" en el siguiente caso: esquema de mi capa de datos donde quiero recopilar la información
Por el momento logré escribir el siguiente código para saber si existe o no un evento SearchResult:
//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; };Ahora estoy luchando por encontrar una manera de seleccionar solo el último evento generado y devolver el valor contenido en "digitalData.event.attributes.pageIndex".
¿Alguien tiene alguna solución con respecto a este punto?
Gracias,
Puede guardar eso en una variable temporalmente y devolver el último resultado. ¿Es esto lo que querías?
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; };Filtre los artículos primero, y si hay alguno, tome el último artículo:
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; };Hola hermano, puedes hacer lo que quieras de esta manera.
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)