lundi 8 avril 2019

Foreach looping problem with if statement

why the console log shows this ?

  • 3 elem :
  • else
  • elem :
  • else
  • elem :
  • else
  • elem :
  • else
  • 2 elem :
  • 4 if

I expected the output :

  • elem
  • if
  • elem
  • else ... I thought that will shows if or else after every elem !

Here is the code :

        res.docs.forEach((elem) => {
          console.log('elem');
          if (elem.productType) {
            this.Service.function(elem.productType._id).subscribe(
              (result) => {
                console.log('if');
                const text = result.name;
                this.marketPlaceList.push(text);
              }
              );
            } else {
              console.log('else');
            this.marketPlaceList.push('');
          }
        });

Aucun commentaire:

Enregistrer un commentaire