在递归javascript函数的结果中追加父级

时间:2013-07-16 15:14:37

标签: javascript arrays json search recursion

如何在结果中的(反向)树中获取父级。

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

谢谢, 雅各布

1 个答案:

答案 0 :(得分:0)

使用DefiantJS(defiantjs.com)进行此类查询非常简单。此lib使用方法“search”扩展全局对象JSON。使用此方法,您可以使用XPath表达式对JSON结构进行查询。

以下是代码的小提琴 http://jsfiddle.net/hbi99/cPRd9/

像这样(我已经缩短了你的JSON以便于概述):

    var data = [
       {
          "title": "Category1",
          "children": [
             { "title": "video 1", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" },
             {
                "title": "Subcategory1",
                "children": [
                   { "title": "video 2", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" },
                   { "title": "video 3", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" },
                   { "title": "video 4", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" }
                ]
             },
             { "title": "video 5", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" },
             {
                "title": "Subcategory2",
                "children": [
                   { "title": "video 5", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" },
                   { "title": "video 6", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" },
                   { "title": "video 7", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" }
                ]
             }
          ]
       },
       {
          "title": "Category3",
          "children": [
             { "title": "video 8", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" },
             {
                "title": "Subcategory1",
                "children": [
                   { "title": "video 9", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" },
                   { "title": "video 10", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" },
                   { "title": "video 11", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" }
                ]
             },
             { "title": "video 12", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" },
             {
                "title": "Subcategory2",
                "children": [
                   { "title": "video 13", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" },
                   { "title": "video 14", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" },
                   { "title": "video 15", "url": "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", "thumb": "*.jpg" }
                ]
             }
          ]
       }
    ],
    res1 = JSON.search( data, '//*[title and url]' ),
    res2 = JSON.search( data, '//*[title = "video 13"]' ),
    str = '';

for (var i=0; i<res1.length; i++) {
    str += res1[i].title +'<br/>';
}

document.getElementById('output').innerHTML = str;

console.log( res2[0] );
// {title: "video 13", url: "http://www.youtube.com/watch?v=Nk5_1OZ4DVI", thumb: "*.jpg"}