代码之家  ›  专栏  ›  技术社区  ›  Angulandy2

Angular 6无法从数组中获取项

  •  -2
  • Angulandy2  · 技术社区  · 7 年前

    我正在构建显示亚马逊产品的应用程序:

    showProducts(){
        this.search.getProducts().subscribe
        ((data: any) => {
            this.list = data['hydra:member'];
            this.prices = data.lowestPrices.Amazon.newItem;
            console.log('Objects', this.list);
            console.log('Prices', this.prices);
        });
    
    }
    

    enter image description here

    无法读取未定义的属性“Amazon”

    2 回复  |  直到 7 年前
        1
  •  2
  •   Nikolai Kiefer    7 年前

    [] . 数组有索引 [0]->[n]

    showProducts(){
     this.search.getProducts().subscribe
     ((data: any) => {
      this.list = data['hydra:member'];
      this.prices = data['hydra:member'][0].lowestPrices.Amazon.newItem;
      console.log('Objects', this.list);
      console.log('Prices', this.prices);
    });
    

    第一项。

    您必须为每个信息的每个数据项编写一个循环

        2
  •  1
  •   Ashish Ranjan    7 年前

    Array map ,以迭代数组并填充属性

    this.prices = data['hydra:member'].map(elem => elem.lowestPrices.Amazon.newItem;);
    
    推荐文章