Angular 无法打印包含在另一个对象列表中的对象列表
我的项目中有以下模型 动物Angular 无法打印包含在另一个对象列表中的对象列表,angular,typescript,angular6,Angular,Typescript,Angular6,我的项目中有以下模型 动物 private name: string; private age: string; private zooLocation: Zoo[]; 动物园 private zooname: string; private zooaddress: address; 在项目中,data返回Animal[]。因此,数据包含动物对象的列表。我只想console.log()所有可用的动物园位置。因此,Animal对象可能包含大约10个对象,其中每个对象可能包含多个Zoo位
private name: string;
private age: string;
private zooLocation: Zoo[];
动物园
private zooname: string;
private zooaddress: address;
在项目中,data
返回Animal[]
。因此,数据包含动物对象的列表。我只想console.log()
所有可用的动物园位置。因此,Animal
对象可能包含大约10个对象,其中每个对象可能包含多个Zoo
位置
我要做的就是打印所有这些Zoo
位置,它们属于Animal
对象
到目前为止,我尝试的是:
this.animalList = data;
this.allZooLocation = this.animalList.zooLocation;
this.animalList.zoologition代码>显示错误
类型“Animal[]”上不存在属性“zooLocation”
这是因为this.animalList
是一个数组,没有zooLocation
属性。
您可以在animalList
中循环,并从Animal
类型中的每个元素获取数据。大概是这样的:
for (let animal of this.animalList) {
console.log(animal.zooLocation);
}
此代码在控制台中打印每只动物的zooLocation
。如果您想对该位置执行其他操作,可以将代码替换为console.log
,这是因为this.animalList
是一个数组,没有zooLocation
属性。
您可以在animalList
中循环,并从Animal
类型中的每个元素获取数据。大概是这样的:
for (let animal of this.animalList) {
console.log(animal.zooLocation);
}
此代码在控制台中打印每只动物的zooLocation
。如果您想对该位置执行其他操作,可以使用console.log
替换代码。animalList基本上是一个数组,而不是json。虽然这个“点”符号在JSON格式下工作,但数组仍然需要一个指向的位置
例如,animalList[0]表示列表第一个位置的对象,依此类推。所以,您要打印的是animalList[i]。zooLocation,其中i的范围从0到(arrayLength-1)
您可以为此使用普通for循环。例:
for(let i=0; i<animalList.length; i++){
console.log(animalList[i].zooLocation);}
for(让i=0;ianimalList基本上是一个数组,而不是json。虽然这个“点”符号在json格式下工作,但数组仍然需要一个指向的位置
例如,animalList[0]表示列表第一个位置的对象,依此类推。因此,您要打印的是animalList[i]。zooLocation,其中i的范围为0到(arrayLength-1)
您可以为此使用普通for循环。例如:
for(let i=0; i<animalList.length; i++){
console.log(animalList[i].zooLocation);}
for(设i=0;i这是我的答案
public AnimalList : Animal[] = [
{
name : 'name1',
age : "20",
zooLocation : [
{ zooname : "zooname1",
zooaddress : "zooaddress1"
},
{ zooname : "zooname2",
zooaddress : "zooaddress2"
},
{ zooname : "zooname3",
zooaddress : "zooaddress3"
},
]
},
{
name : 'name2',
age : "21",
zooLocation : [
{ zooname : "zooname4",
zooaddress : "zooaddress4"
},
{ zooname : "zooname5",
zooaddress : "zooaddress5"
}
]
}
]
在模板中
<div *ngFor="let list of AnimalList">
<div>{{list.name}}</div>
<div *ngFor="let item of list.zooLocation">
{{item.zooname}}
{{item.zooaddress}}
</div>
</div>
或者你可以在你的.ts中使用它
for(let j=0; j<this.AnimalList.length; j++){
console.log(this.AnimalList[j].zooLocation);
}
这是我的答案
public AnimalList : Animal[] = [
{
name : 'name1',
age : "20",
zooLocation : [
{ zooname : "zooname1",
zooaddress : "zooaddress1"
},
{ zooname : "zooname2",
zooaddress : "zooaddress2"
},
{ zooname : "zooname3",
zooaddress : "zooaddress3"
},
]
},
{
name : 'name2',
age : "21",
zooLocation : [
{ zooname : "zooname4",
zooaddress : "zooaddress4"
},
{ zooname : "zooname5",
zooaddress : "zooaddress5"
}
]
}
]
在模板中
<div *ngFor="let list of AnimalList">
<div>{{list.name}}</div>
<div *ngFor="let item of list.zooLocation">
{{item.zooname}}
{{item.zooaddress}}
</div>
</div>
或者你可以在你的.ts中使用它
for(let j=0; j<this.AnimalList.length; j++){
console.log(this.AnimalList[j].zooLocation);
}
for(设j=0;jError非常清楚,您试图访问的是数组上的属性,而不是对象上的属性。它应该像this.animalList[0]。zooLocation
错误非常清楚,您试图访问的是数组上的属性,而不是对象上的属性。它应该像this.animalList[0].zooLocation
如果我只想打印zooname.So console.log(animal.zooLocation.zooname),该怎么办;不起作用。它不起作用,因为与animalList
类似,zooLocation
是一个类型为Zoo
的元素数组。因此,您应该像我为animalList
所做的那样。如果我只想打印zooname.so console.log(animal.zooLocation.zooname),该怎么办;不起作用。它不起作用,因为与animalList
类似,zooLocation
是一个类型为Zoo
的元素数组。因此,您应该像我为animalList
所做的那样。如果我只想打印zooname.so console.log(animal.zooLocation.zooname),该怎么办;不起作用。如果我只想打印zooname.So console.log(animal.zooLocation.zooname);该怎么办。