我对在 angular2 中使用数组的概念完全陌生。我的 JSON 数据来自后端,我将其存储在名为“arr”的数组中。但我想访问“arr”中每个对象中存在的数组,即“arr”中的每个对象都有一个数组,其中包含一些元素。我想在 .ts 文件而不是 HTML 文件中访问该数组。我需要对 .ts 文件中的内容进行一些修改,因此我想知道如何为其创建管道。
我读到我们需要使用管道。但我不明白如何使用管道来实现这一点。 如果您在图片中看到,有一个主数组,其中有 3 个对象,这 3 个对象中的每一个都有一个名为categories 的数组,该数组也有对象。我想访问主数组中存在的所有 3 个对象的类别内的“类别”字段。有人可以帮我做吗?
最佳答案
如果是html格式
<div *ngFor="let item of arr">
{{item.aliasname}}
<div *ngFor="let category of item.categories">
{{category.name}}
</div>
</div>
在类型脚本中,您可以使用 foreach 循环
arr.forEach(element => {
element.forEach(category => {
console.log(category.name);
// Do whatever you want
// You can access any field like that : category.yourField
});
});
关于Angular2、Ionic2 :How to access an array of objects inside another array of objects in typescript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43231315/