arrays - 使用 *ngFor (Angular 4) 遍历 JSON 对象数组

标签 arrays json angular ngfor

我想遍历我的 json 文件中的对象数组。

JSON

[
  {
    "name": "Mike",
    "colors": [
      {"name": "blue"},
      {"name": "white"}
    ]
  },

  {
    "name": "Phoebe",
    "colors": [
      {"name": "red"},
      {"name": "yellow"}
    ]
  }
]

html

<div *ngFor="let person of persons">
   <p>{{person.name}}</p> <!-- this works fine-->
   <p *ngFor="let color of person.colors"> <!--I want to list the colors of the person here-->
     {{color.name}}
   </p>
</div>

我无法访问一个人的颜色数组。我该如何实现?

我已经看过这些帖子,但它们的解决方案对我没有帮助:

Angular2 ngFor Iterating over JSON

Angular2 - *ngFor / loop through json object with array

最佳答案

对于 Angular 6.1+ ,您可以使用默认管道 keyvalue ( Do review also ) :

<ul>
    <li *ngFor="let recipient of map | keyvalue">
        {{recipient.key}} --> {{recipient.value}}
    </li>
</ul>

WORKING DEMO


以前:正如您所说:

Angular2 - *ngFor / loop through json object with array ,这帮不了你

你不需要那个,因为你的代码工作正常,请检查

WORKING DEMO

关于arrays - 使用 *ngFor (Angular 4) 遍历 JSON 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48402215/

相关文章:

php - 如何找出是否在嵌套数组中

javascript - 使用对象和函数从数组中删除对象

javascript - 将多个对象缩减为一个,将值加在一起

Android volley 如何接收和发送一个 json

javascript - 有没有办法创建与 JSON.stringify(jsObject) 创建的 Java <-> JSON 映射对象完全相同的字符串?

css - Primeng Datatable - 在该行中单击按钮时如何向特定 tr 元素添加样式

angular - Angular 2 中 Zone.js 的用途是什么

javascript - 检查对象属性值是否为 false

java - 使用 java 从动态 json 中查找键的值

angular - Ng2智能表,从一个对象中获取多列