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

189
Views
All objects ID's are the same when pushing new elements to an array

I am executing the following code:

        dietDay.meals.forEach((meal) => {
          meal.mealProducts.forEach((mealProduct) => {
            if (
              mealProduct.product.id &&
              this.fetchedProductIds.includes(mealProduct.product.id)
            ) {
              return;
            } else if (mealProduct.product.id) {
              this.fetchedProductIds.push(mealProduct.product.id);
              this.httpClient.get(environment.backendUrl + '/products/' + mealProduct.product.id)
              .subscribe((response: any) => {
                this.products.push(response);
                this.setDaySummary();
                this.setActiveMealSummary();
              });
            }
          });
        });
      });

The loop iterates through meals, and gets the product data from the backend using a GET call.

this.products array contains an array of objects of type Products. In each iteration a new Product is downloaded from the backend and pushed into the array. After running through the loop all of the Products in the array have the same ID, but all of the other properties are different.

enter image description here

As you can see in the console log output of the this.products array, two objects have the same ID, even though when I console.log output each individual response from the backend the ID's are different.

What is going on?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In the this.setDaySummary() function, I had '=' instead of '===' and it was assigning the product id.

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!