0

How am i able to get the parent(s) in the (reverse) tree in the results.

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;
}

I tried the following but that will deliver me more than 10** results(objects),

//recursive case
if(typeof arr[k] === 'object'){
   results = results.concat(arr[k]);
   results = results.concat(find(needle, arr[k]));
}

Edit:

Current result:

0: Object
thumb: "*.jpg"
title: "sometitle"
url: "http://www.youtube.com/watch?v=Nk5_1OZ4DVI"

Expected result: For example:

   0: Object
     title: "Category1"
      children: Array[1]
        0: Object

Thanks, Jacob

4
  • Your question is a bit unclear. Could you post 1) source data (jsonstring) 2) expected results?
    – georg
    Commented Jul 16, 2013 at 15:17
  • Why the concat? Just go for return find(needle, arr[k]);. Also, iterate through in array using for(var i = 0; i < array.length; i++) { [...] }.
    – Amberlamps
    Commented Jul 16, 2013 at 15:21
  • 1
    If you're declaring a variable with JavaScript object literal syntax, there's no need to invoke a JSON parser. Also, don't use for ... in loops for arrays. Also declare "results" with var!
    – Pointy
    Commented Jul 16, 2013 at 15:22
  • Thanks for the coding suggestions, always a good practice, i added the json string and expected result. Bit of a bad notation sry..about that
    – Jacob
    Commented Jul 16, 2013 at 15:55

1 Answer 1

0

Such queries with DefiantJS (defiantjs.com) are trivial. This lib extends the global object JSON with the method "search". Using this method, you can make queries on JSON structures with XPath expressions.

Here is the fiddle for the code below
http://jsfiddle.net/hbi99/cPRd9/

Like this (I've shortened your JSON to facilitate overview):

    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"} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.