我如何能够在结果中的(反向)树中获得父级。
var json = '[{"title":"Category1","children":[{"title":"sometitle","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"Subcategory1","children":[{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"}]},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"Subcategory2","children":[{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"}]}]},{"title":"Category2","children":[{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"Subcategory1","children":[{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"}]},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"Subcategory2","children":[{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"}]}]},{"title":"Category3","children":[{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"Subcategory1","children":[{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"}]},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"Subcategory2","children":[{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"}]}]},{"title":"Category4","children":[{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"Subcategory1","children":[{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"}]},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"Subcategory2","children":[{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"},{"title":"video","url":"http:\/\/www.youtube.com\/watch?v=Nk5_1OZ4DVI","thumb":"*.jpg"}]}]}]';
var result = find('sometitle',json);
console.log(result);
function find(needle, arr){
var results = [];
for(var k in arr){
//recursive case
if(typeof arr[k] === 'object'){
results = results.concat(find(needle, arr[k]));
}
//base case
if(arr[k]==needle){
return arr;
}
}
//base case
return results;
}
我尝试了以下方法,但这会给我带来超过 10** 个结果(对象),
//recursive case
if(typeof arr[k] === 'object'){
results = results.concat(arr[k]);
results = results.concat(find(needle, arr[k]));
}
编辑:
当前结果:
0: Object
thumb: "*.jpg"
title: "sometitle"
url: "http://www.youtube.com/watch?v=Nk5_1OZ4DVI"
预期结果: 例如:
0: Object
title: "Category1"
children: Array[1]
0: Object
谢谢,雅各布