简体   繁体   English

forEach 角度循环

[英]forEach angular loop

data:(4) [{…}, {…}, {…}, {…}]
included:{brands: Array(2), main_images: Array(4)}

I am currently logging that to the console.我目前正在将其记录到控制台。 However within data is my products name and within included in my products href to their respective images.但是在数据中是我的产品名称,在我的产品href 中包含在它们各自的图像中。

In my .ts file:在我的 .ts 文件中:

 getAllProducts() {
    this.mhttp.allProducts()
      .subscribe((data: any) => {
        this.names = data.data;
        this.products = data.included.main_images;
        console.log(data);
      },
        error => {
          console.log(error);
        });
  }

in my html file:在我的 html 文件中:

<div class="row">
    <div class="col-sm-12">
      <div class="row">
        <div class="col-sm-4 products" *ngFor="let product of products">
          <div class="image">
            <img [src]="product.link.href">
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-sm-4 products" *ngFor="let name of names">
          <div class="name">
            <a routerLink="/product/viewProduct/{{name.id}}">
              <h3>{{ name.name | uppercase }}</h3>
            </a>
          </div>
        </div>
      </div>
    </div>
  </div>

at the moment I am looping through my images and them my product names, so the product names are not underneath they product images.目前我正在循环浏览我的图像和我的产品名称,因此产品名称不在它们的产品图像下方。

It came to my attention that forEach loops will be best.我注意到 forEach 循环将是最好的。 But I am not 100% sure how to forEach loop through different objects.但我不是 100% 确定如何 forEach 循环遍历不同的对象。

You could combine them into one array using something like the below.您可以使用如下所示的方法将它们组合成一个数组。

   getAllProducts() {
    this.mhttp.allProducts()
      .subscribe((data: any) => {
        this.products = data;
        this.productList = this.products.data.map(product => {
        //get the image id out of the product object
          const imageId = product['relationships'].main_image
            ? product['relationships'].main_image.data.id
            : false
            //now loop through and match them to the included data
           return {
            ...product,
            //id from product id to the the id of the included then add to product
            image: imageId
              ? this.productIncluded['main_images'].find(img => img.id === imageId).link.href
              : '/static/cat.svg'
            }
          }),
    error => {
      console.log(error);
        });
     }

Then in your html everything should be in the product list array to be used.然后在您的 html 中,所有内容都应该在要使用的产品列表数组中。

I know this question was asked long ago, but after reviewing my questions ond stackoverflow I remembered the solution was to map the two objects into one:我知道很久以前就有人问过这个问题,但是在查看我的问题和 stackoverflow 之后,我记得解决方案是将两个对象映射为一个:

getAllProducts() {
    this.mhttp.allProducts()
      .subscribe((data: any) => {
        this.products = data;
        this.prodList = this.products.data.map((itm, i) => {
          return {
            title: itm.name,
            imgUrl: this.products.included.main_images[i].link.href,
            price: itm.meta.display_price.with_tax.formatted,
            id: itm.id
          };
        });
        console.log(this.products);
      },
        error => {
          console.log(error);
        });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM