Get depth of object - Node.js Object

Node.js examples for Object:Object Operation

Description

Get depth of object

Demo Code

function depthOf(object) {
  var level = 1;// w w w .  ja v  a 2 s.  co m
  var key;
  for(key in object) {
    if (!object.hasOwnProperty(key)) continue;

    if(typeof object[key] == 'object'){
      var depth = depthOf(object[key]) + 1;
      level = Math.max(depth, level);
    }
  }
  return level;
}

Related Tutorials