2

I have a nodeJSON string like

{
  "Name": "Addition",
  "Id": "3",
  "ParentId": "1",
  "children": [
    {
      "Name": "Two Numbers",
      "Id": "5",
      "ParentId": "3",
      "children": []
    },
    {
      "Name": "Three Numbers",
      "Id": "6",
      "ParentId": "3",
      "children": []
    }
  ]

}

I get a parentId as 3 then i want to display the Key value name as "Addition" which should be matched with its Id.

{
      "Name": "Addition",
      "Id": "3",
      "ParentId": "1",
      "children": [....]
}

myApp.controller('leafController', function($scope){

$scope.getname = function(parentid,nodeJSON )
 { 
   //parentid is 3 here
    $scope.Id = parentid; 
    $scope.data = nodeJSON ;
    // i want to get name according to id
    // Name : Addition (if Id is 3) 

 };


  });

I am struck here.

Please help me,
thanks in advance

Seetha
  • 365
  • 1
  • 6
  • 20
  • possible duplicate of [How to parse JSON in JavaScript](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript) – John Dvorak Dec 18 '13 at 14:13

1 Answers1

0

Loop on the children array and return the children with Id = 3, a function like:

function getChildrenById(children, value) {
     for (var i=0, iLen=children.length; i<iLen; i++) {

        if (arr[i].Id == value) return arr[i];
      }
    }
}

And call it like getChildrenById(children, 3)

Atropo
  • 12,231
  • 6
  • 49
  • 62
  • It is not working i want to pass the entire json string to the function and need to find the name by id. Is there any other way to proceed? – Seetha Dec 18 '13 at 10:39