Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!