-1

I want to populate an array ( $scope.children= [];) with all the children extracted from this array format (JSON data from a GET). Note that there is no top-level label.

[
  {
    "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"
    ]
  }

]
mplungjan
  • 169,008
  • 28
  • 173
  • 236
amartinez
  • 169
  • 2
  • 14

1 Answers1

0

Very simple, just use map function to return all the children data

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

then in your Angular html you can do a nested view

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

or the fastest way just use the angular ng-repeat

<div ng-repeat="item in data"> 
    <label ng-repeat="value in item.children">{{value }}</label>
</div> 
Tim
  • 3,755
  • 3
  • 36
  • 57