Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
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 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda