我有一个类似
的nodeJSON字符串{
"Name": "Addition",
"Id": "3",
"ParentId": "1",
"children": [
{
"Name": "Two Numbers",
"Id": "5",
"ParentId": "3",
"children": []
},
{
"Name": "Three Numbers",
"Id": "6",
"ParentId": "3",
"children": []
}
]
}
我得到一个parentId为3然后我想将Key值名称显示为“Addition”,它应与其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)
};
});
我很震惊。
请帮助我,
提前谢谢
答案 0 :(得分:0)
在children
数组上循环,并使用Id = 3
返回子项,函数如下:
function getChildrenById(children, value) {
for (var i=0, iLen=children.length; i<iLen; i++) {
if (arr[i].Id == value) return arr[i];
}
}
}
并将其称为getChildrenById(children, 3)