Angular 6 无法从数组中获取项目
Angular 6 cannot get item from array
我正在构建显示亚马逊产品的应用程序:
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);
});
}
this.list 在控制台中:
this.prices 在控制台中:
Cannot read property 'Amazon' of undefined
如何正确获取亚马逊最低价?
您正在访问数组 []
。数组有索引 [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);
});
第一项。
您必须为每个信息的每个数据项编写一个循环
使用 Array map 遍历数组并填充您的属性
例如:
this.prices = data['hydra:member'].map(elem => elem.lowestPrices.Amazon.newItem;);
我正在构建显示亚马逊产品的应用程序:
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);
});
}
this.list 在控制台中:
this.prices 在控制台中:
Cannot read property 'Amazon' of undefined
如何正确获取亚马逊最低价?
您正在访问数组 []
。数组有索引 [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);
});
第一项。
您必须为每个信息的每个数据项编写一个循环
使用 Array map 遍历数组并填充您的属性 例如:
this.prices = data['hydra:member'].map(elem => elem.lowestPrices.Amazon.newItem;);