Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular 6无法从数组中获取项_Javascript_Angular_Typescript_Angular Http - Fatal编程技术网

Javascript Angular 6无法从数组中获取项

Javascript Angular 6无法从数组中获取项,javascript,angular,typescript,angular-http,Javascript,Angular,Typescript,Angular Http,我正在构建显示亚马逊产品的应用程序: showProducts(){ this.search.getProducts().subscribe ((data: any) => { this.list = data['hydra:member']; this.prices = data.lowestPrices.Amazon.newItem; console.log('Objects', this.list); c

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

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);
    });

}
控制台中的此列表:

这是控制台中的价格:

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


如何正确获得亚马逊的最低价格?

您正在访问一个数组
[]
。数组具有索引
[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);
});
第一项

您必须为每个信息使用的每个数据项编写一个循环,以迭代数组并填充属性 例如:

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

那不是一个
数组吗?首先转到索引,然后检索propExample将帮助尝试数据[0]['lowestPrices']['Amazon]数据[index].lowestPrices.Amazon.newItem;使用此
data['hydra:member'][index].Amazon.newItem
不是一个数字,而是一个具有包含欧元价格的
FR
属性的对象。由于未显示其他数据,因此可能有一个新项目实例具有与Euro不同的值的
EN
GBP
属性。如果不知道可能的数据,就无法知道最低价格。无法获取未定义的newItem。我使用它:data['hydra:member'].Amazon.newItem;this.prices=data['hydra:member'][0].lowestPrices.Amazon.newItem;为我工作你是如何在html文件中访问它的?对于(i)或.map哪个更好?节省空间是我唯一想说的更好的事情then@Angulandy2我很抱歉刚刚读到:@Angulandy2:您甚至可以看到:似乎
for循环
映射
的性能取决于我们迭代的数据大小