0

I've been trying to figure this out for quite some time now. I'm to a point where I'm having dreams about it. So, my input is:-

var obj = {
   "one": {
     "propOne": 1,
     "propTwo" : {
       "innerProp" : "hello"
   },
   "two": {
     "twoProp": "something",
     "twoProp2": {
       "inside": "world",
       "deep": {
         "end": "ok"
       }
     }
   }
}

Pretty standard object. What I'm trying to do is traverse and get the key with property if property's value is an object. So, my output would be something like this:-

one
one.propTwo
one.propTwo.innerProp
two
two.twoProp2
two.twoProp2.deep
two.twoProp2.deep.end

So far I've been able to get all the properties whose value is object but what I'm struggling on is trying to get the exact structure as shown on the output above.

I have this so far:-

function isObject(obj) {
  'use strict';
  var result = false;
  if (typeof obj === 'object' && obj !== null && Object.keys(obj).length > 0) {
    result = true;
  }
  return result;
}

function recurse(obj, key) {  
  'use strict';
  key = key || '';
  if (isObject(obj)) {
    for (var k in obj) {
      if (isObject(obj[k])) {
        var dot = key ? '.' : '';
        key += key ? '.' + recurse(obj[k], k) : recurse(obj[k], k);
      } else {
      }
    }
  }
  return key + ',';
}

If you run the code above it's quite clear the output is far from what I want but it does get all the necessary properties which is needed to be displayed. I'm just stomped on trying to get it display exactly like I want it. Any pointer would be appreciated. It doesn't have to be recursive. I just thought it was easier to achieve this with recursion.

Fiddle here


@Tomalak is right that this is a duplicate of the linked post but I'm posting a generic solution rather than the one posted in the original post.

var stack = [];

function saveStack(arr) {
  stack.push(arr);
}

function recurse(obj, key) {
  'use strict';
  key = key || '';
  for (var k in obj) {
    if (obj.hasOwnProperty(k)) {
      if (isObject(obj[k])) {
        var dot = key ? '.' : '';
        var stack = key + dot + k;
        saveStack(stack);
        recurse(obj[k], stack);
      }
    }
  }
}
shriek
  • 5,605
  • 8
  • 46
  • 75
  • Could you provide a jsFiddle or use stackoverflow's capabilities to run the code? – mic4ael Oct 04 '15 at 14:41
  • A better `isObject` would be `Object(obj) === obj`. Functions are also objects but `typeof` says `"function"`, so you get a false negative. – Oriol Oct 04 '15 at 14:46
  • Your example output seems to follow an inconsistent rule, but whatever. I think your main problem is that concatenating the key needs to be outside the loop. – Paul Kienitz Oct 04 '15 at 14:48
  • There are so many duplicates of this that it's hard to pick one as the close target. – Tomalak Oct 04 '15 at 14:51
  • @Oriol, thanks for the tip although I don't really care about how we get a valid object as long as it's a JSON object. – shriek Oct 04 '15 at 15:01
  • @Tomalak, Hm..I was looking at the same post the other day. I don't know how I missed it. The only difference between that post and mine would be that I'm only listing keys that have object while that one lists out all the keys. I'll go ahead and delete this post if I actually find what I'm looking for from that post. – shriek Oct 04 '15 at 15:01
  • 1
    @shriek As far as I can see, it's the same thing. You can leave your post online. It will help push the other one higher in search engine rankings, which is not a bad thing. – Tomalak Oct 04 '15 at 15:16
  • 1
    @Tomalak, You were absolutely right. I must have been overthinking this too much. I did however tweaked this to my own liking so that it's more generic. – shriek Oct 04 '15 at 18:52

0 Answers0