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

133
Vistas
Tried my best to stop duplicating data on continuous http request by clicking refresh button(for that particular component) Angular 5

The below is my .ts file for the Alarm Component and over HTML I am using a simple *ngFor over criticalObject.siteList to display the records This is not the original code I have simplified this but the problem I am facing is that on rigorous click on the refresh button(fires HTTP request), the list is adding duplicate siteNames and that should not happen. I have heard of debounce time, shareReplay, and trying applying here, which even doesn't make sense here. NOTE: I have to fire the HTTP request on every refresh button click. Keenly Waiting for Help.

criticalObject.siteList = [];
siteList = ["c404", "c432"];
 
onRefresh() {
 this.criticalObject.siteList = [];
 this.siteList.forEach(elem => {
  getAlarmStatus(elem);
 })
}

getAlarmStatus(item) {
 critical_list = [];
 alarmService.getAlarmStatusBySite(item.siteName).subcribe(data => {
  if(data) {
  // do some calculations
   if(this.criticalObject.siteList.length === 0) {
     this.criticalObject.siteList.push({
     siteName = item.siteName;
    })
   }
   this.criticalObject.siteList.forEach((elem, idx) => {
    if(elem.siteName === item.siteName) {
     return;
    } else if(idx === this.criticalObject.siteList.length - 1) {
      this.criticalObject.siteList.push({
       siteName = item.siteName;
       })
    }

   })
   }
  }
})

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

0

I did a silly mistake, I am new to JavaScript, I found out you cannot return from a forEach loop and that's why I was getting duplicated records, return statement in forEach acts like a continue in JavaScript.

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