Angular2、Ionic2 :How to access an array of objects inside another array of objects in typescript?

标签 angular object typescript ionic2 arrayofarrays

我对在 angular2 中使用数组的概念完全陌生。我的 JSON 数据来自后端,我将其存储在名为“arr”的数组中。但我想访问“arr”中每个对象中存在的数组,即“arr”中的每个对象都有一个数组,其中包含一些元素。我想在 .ts 文件而不是 HTML 文件中访问该数组。我需要对 .ts 文件中的内容进行一些修改,因此我想知道如何为其创建管道。

我读到我们需要使用管道。但我不明白如何使用管道来实现这一点。 如果您在图片中看到,有一个主数组,其中有 3 个对象,这 3 个对象中的每一个都有一个名为categories 的数组,该数组也有对象。我想访问主数组中存在的所有 3 个对象的类别内的“类别”字段。有人可以帮我做吗?

This is how my data looks

最佳答案

如果是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
    });
});

An other way to foreach in Angular2

关于Angular2、Ionic2 :How to access an array of objects inside another array of objects in typescript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43231315/

相关文章:

node.js - Node 错误 npm ERR!在 Angular 项目中使用 npm install 时从未调用 cb()

angular - 当 Ionic Ecommerce App 中购物车中的产品数量增加时,总价不会更新

java - 用于创建名称递增的对象的数组

javascript - 使用 Jasmine toEqual 未定义预期对象

node.js - 如何扩展nodejs模块的typescript定义

typescript - RxJS typescript : Property 'reduce' does not exist on type 'Observable<number>'

Django Angular cors 错误 : access-control-allow-origin not allowed

json - 不能在 *ngFor : angular2 中使用 *ngIF

Javascript:如何过滤对象数组和求和结果

typescript :我可以定义一个长度为 n 的元组类型吗?