Ad

Get Fully Qualified Name Of Current JSON Node

- 1 answer

Given a Javascript object, how can I iterate through each property AND get the fully qualified name of each node? I am trying something like this:

var test = {
    "axes" : [{ 
        "stackType": "100%", 
        "testObject": {
            "testProperty": "found"
        },
        "minimum": 0
    }, {
        "gridColor": "#test", 
        "testArray": [
            {"jelly": 2}, {"jam": 3}
        ],
        "gridAlpha": 1
    }]
};

iterate(test);

function iterate(obj, parents) {

    if(!parents) { var parents = []; }

    for (var property in obj) {

        parents.push(property);

        if (obj.hasOwnProperty(property)) {

            if (typeof obj[property] == "object") { 
                iterate(obj[property], parents);
            } 
            else if (obj[property].constructor == Array) {
                for (var i = 0; i < obj[property].length; i++) {
                    iterate(obj[property][i], parents);
                }
            }
            else {
                console.log(parents + " : " + obj[property]);
            }

        }

    }

}

But the parents array just keeps getting added to. I think it's because Javascript handles the array by reference instead of by value.

My desired output would be something like this:

axes, 0, stackType : 100%
axes, 0, testObject, testProperty : found
axes, 0, minimum : 0
axes, 1, gridColor: #test
axes, 1, testArray, 0, jelly: 2
axes, 1, testArray, 1, jam: 3
axes, 1, gridAlpha: 1

but instead I just end up with each new property added, and the list never resets.

Ad

Answer

Inside your loop, you need to create a copy of the passed in parents array and append the property to it, and pass that in the recursive call. Otherwise you are just modifying the same array.

var test = {
  "axes": [{
    "stackType": "100%",
    "testObject": {
      "testProperty": "found"
    },
    "minimum": 0
  }, {
    "gridColor": "#test",
    "testArray": [{
      "jelly": 2
    }, {
      "jam": 3
    }],
    "gridAlpha": 1
  }]
};

iterate(test);

function iterate(obj, parents) {

  if (!parents) {
    var parents = [];
  }

  for (var property in obj) {

    // changed line:
    var path = [].concat(parents, property);

    if (obj.hasOwnProperty(property)) {

      if (typeof obj[property] == "object") {
        iterate(obj[property], path); // changed
      } else if (obj[property].constructor == Array) {
        for (var i = 0; i < obj[property].length; i++) {
          iterate(obj[property][i], path); // changed
        }
      } else {
        // changed so you can see it without opening the console
        logLine(path.join(', ') + " : " + obj[property]);
      }

    }

  }
}

function logLine(line) {
  // show in console and in HTML
  console.log(line);
  document.getElementById('output').textContent += line + '\n';
}
<pre id="output"></pre>

Ad
source: stackoverflow.com
Ad