0

As my Title, I'd like to get parent data from nested json. I know how to do it in HTML. And I'd like to know how to do it in controllers.

Here is my directive with nested json

.factory('dataFac', function(){
return {
    parents : [{
        name: 'George',
        age: 44,
        children: [{
            name: 'Jack',
            age: 16
        },{
            name: 'Amy',
            age: 13
        }]
    }, {
        name: 'Jimmy',
        age: 38,
        children: [{
            name: 'Max',
            age: 7              
        },{
            name: 'Lily',
            age: 5
        },{
            name: 'Kim',
            age: 4
        }]
    }]
}})

Controller

.controller('dataCtrl', function($scope, $location, dataFac) {
  $scope.data = dataFac.parents;

  //here I'd like to get name of parents
  $scope. = ;

  //here I'd like to get name of children
  $scope. = ;

  //and when I get the data of the children, I can get their parents' name
  $scope. = ;

  console.log($scope.navbars.id);

  $scope.isActive = function(route) {
      return route === $location.path();
  };})

Appreciate if someone could help.

3
  • isn't parent name is in $scope.data[i].name ? Commented May 19, 2015 at 2:52
  • @TuanAnhTran If I want to get all of parents name? Commented May 19, 2015 at 3:00
  • looop through the $scope.data then? Commented May 19, 2015 at 3:24

1 Answer 1

2

You can get the name of parents and the children names using forEach function in angularjs and looping through the json data.,

Here is the code

app.controller('myController', function($scope, dataFac) {
 $scope.data = dataFac.parents;
 $scope.parentsName = [];
 $scope.childName = [];
//here I'd like to get name of parents

 angular.forEach(dataFac.parents, function(parent) {
    $scope.parentsName.push({
     name: parent.name
    });
     angular.forEach(parent.children, function(child) {
      //here I'd like to get name of children

      $scope.childName.push({
       childName: child.name,
       parentName: parent.name
      });
    });
 });
});

Update: If you want, push parent name into children array and access it from there.

Here is the working plunker with the code

http://embed.plnkr.co/9HCJy554oTSKxrdtiNR4/preview

Hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! It works well! And there's still a question, what if I want to get parents' name from children's array?
@Gloria i take that the children names are unique. You can loop through parents, then loop through children list again, return when found. It's pretty straight forward.
@AlhuckA Thank you so much for the answer. I thought I make it complicated. It's very clear!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.