Javascript 如何获取Json对象的后代
如何获取json对象的所有后代并对其进行过滤。下面是一个c代码,我希望能够在角度方面做到这一点。它的某些部分是Linq。我正在尝试选择所有叶子树Javascript 如何获取Json对象的后代,javascript,angularjs,json,Javascript,Angularjs,Json,如何获取json对象的所有后代并对其进行过滤。下面是一个c代码,我希望能够在角度方面做到这一点。它的某些部分是Linq。我正在尝试选择所有叶子树 e.g. if the following is the input, the answer could be at any level deep. { "title": "first question?", "yes": {"title": "answer A" }, "no": { "title": "second qu
e.g. if the following is the input, the answer could be at any level deep.
{
"title": "first question?",
"yes": {"title": "answer A" },
"no": {
"title": "second question?",
"yes": {
"title": "thirsd question?",
"yes": {
"title": "Fifth question?",
"yes": {
"title": "fourth question",
"yes": {"title": "answer D"},
"no": {
"title": "another question?",
"yes": { "title": "answer E" },
"no": {"title": "answer F"}
}
},
"no": {"title": "answer B"}
},
"no": {"title": "Answer F"}
},
"no": {"title": "Answer G"}
}
}
The output would be:
["answer A", "answer B", "answer D", "Answer F", "Answer G", "answer E"]
检查此代码段
var obj={
“标题”:“第一个问题?”,
“是”:{“标题”:“回答A”},
“否”:{
“标题”:“第二个问题?”,
“是”:{
“标题”:“第三个问题?”,
“是”:{
“标题”:“第五个问题?”,
“是”:{
“标题”:“第四个问题”,
“是”:{“标题”:“答案D”},
“否”:{
“标题”:“另一个问题?”,
“是”:{“标题”:“答案E”},
“否”:{“标题”:“答案F”}
}
},
“否”:{“标题”:“答案B”}
},
“否”:{“标题”:“答案F”}
},
“否”:{“标题”:“回答G”}
}
}
var标题=[];
盖蒂特尔(obj);
函数getTitle(obj){
var keyLen=Object.keys(obj).length;
Object.keys(obj).forEach(函数(key){
if(keyLen==1&&key==title)
标题.推送(obj[键]);
else if(对象的obj[key]实例)
getTitle(obj[key]);
});
}
控制台日志(标题)
那么您想将所有没有键的标题提取到数组中?所有没有同级的标题。因为有些标题只是问题。