Search code examples
javascriptobject-literal

Iterate and search values in a object-literal having multiple levels


Here is what I want to do:

  1. I have a tree (javascript object-literal structure) with multiple levels.
  2. I have a value of a particular key of this object.
  3. I want to search for this exact key-value pair in the structure and return the value of another key as an output.

For clarity following is my object literal:

{
"nodeId": 1081,
"appId": 150,
"displayText": "Welcome here",
"Nodes": [
    {
        "nodeId": 2000,
        "appId": 150,
        "displayText": "Buy",
        "parentNodeId": 1081,
        "Nodes": [
            {
                "nodeId": 2003,
                "appId": 150,
                "displayText": "tCars",
                "parentNodeId": 2000,
                "Nodes": [
                    {
                        "nodeId": 2006,
                        "appId": 150,
                        "displayText": "Diesel",
                        "parentNodeId": 2003,
                        "Nodes": [
                            {
                                "nodeId": 2008,
                                "appId": 150,
                                "displayText": "Price", //This is what I want as return value.
                                "parentNodeId": 2006,
                                "Nodes": [],
                                "nodeCode": "RN_1_1_2_1_3_2_4_1",
                                "parentCode": "RN_1_1_2_1_3_2",
                                "jumpToNode": "RN_1_1" //here is the value that I have with me.
                            }
                        ],
                        "nodeCode": "RN_1_1_2_1_3_2",
                        "parentCode": "RN_1_1_2_1"
                    }
                ],
                "concatWithHeader": false,
                "nodeCode": "RN_1_1_2_1",
                "parentCode": "RN_1_1"
            }
        ],
        "nodeCode": "RN_1_1",
        "parentCode": "RN"
    }
  ],
  "nodeCode": "RN",
  "parentCode": "ROOT_NODE"
}

2. Value that I have with me is "RN_1_1" against jumpToNode

3. I want to search in this object literal and get the value of the key displayText

I searched and tried few things for this but couldnt get the logic to iterate over the inner Nodes objects.

Method I wrote so far:

function getObjects(tree){
var searchkey="RN_1_1";
var displayText = "displayText";
var nodeCode = "nodeCode";
var returnText;

if (tree.hasOwnProperty(nodeCode)) {
   var obj = tree[nodeCode];

   if(obj == searchkey){
    returnText = tree[displayText]; //gives me the return text
    break;
   }
   else{
    //here I should iterate over the inner `Nodes` and get the required value.
   }
}
}

Please help.

Thanks.


Solution

  • I think you can do something like this which works recursively:

    function findProperty(obj, prop, val, propToFetch) {
        var answer;
        if (obj.hasOwnProperty(prop) && obj[prop] === val) {
            return obj[propToFetch];
        }
        for (var i = 0, len = obj.Nodes.length; i < len; i++) {
            answer = findProperty(obj.Nodes[i], prop, val, propToFetch);
            if (answer !== null) {return answer;}
        }
        return null;
    }
    
    var result = findProperty(data, "jumpToNode", "RN_1_1", "displayText");
    

    Working demo here: http://jsfiddle.net/jfriend00/EjC5V/