从JSON子数组中提取值

时间:2015-06-06 15:35:21

标签: javascript json angularjs

我想填充一个数组($ scope.children = [];),其中包含从此数组格式中提取的所有子项(来自GET的JSON数据)。请注意,没有顶级标签。

[
  {
    "tenant_id": 38,
    "name": "Larry",
    "children": [
      "mike"
    ]
  },
  {
    "tenant_id": 89,
    "name": "Moe",
    "children": [
      "paul"
    ]
  },
  {
    "tenant_id": 95,
    "name": "Laurel",
    "children": null
  },
  {
    "tenant_id": 108,
    "name": "Mark",
    "children": [
      "louise"
    ]

  {
    "tenant_id": 39,
    "name": "Hardy",
    "children": [
      "trevor",
      "alison"
    ]
  }

]

1 个答案:

答案 0 :(得分:0)

非常简单,只需使用map函数返回所有子数据

//Return all the children 
data = data.map(function(item) {
    return item.children;
})

然后在你的Angular html中你可以做一个嵌套视图

<div ng-repeat="chilren in data"> 
    <label ng-repeat="item in chilren">{{item}}</label>
</div> 

或以最快的方式使用角度ng-repeat

<div ng-repeat="item in data"> 
    <label ng-repeat="value in item.children">{{value }}</label>
</div>