1

I'm trying to get all the key names 2 levels into an object (potentially further in in the future).

For example given an object like:

myThing = {
   'someKeyTopLevel1': {
      'someKeySecondLevel1': [
         ... very large data here ...
      ],
     'someKeySecondLevel5': [
         ... very large data here ...
      ]
   },
   'someKeyTopLevel2': {
      'someKeySecondLevel1': [
         ... very large data here ...
      ],
      'someKeySecondLevel2': [
         ... very large data here ...
      ],
      'someKeySecondLevel3': [
         ... very large data here ...
      ]
   }
   ...
}

The desired result would be an array of 'someKeySecondLevel1', 'someKeySecondLevel2', 3, 5, etc.

I was thinking of doing something like (pseudo-code):

  • Grab all keys from top level (Object.keys(myThing) )
  • Iterate through those keys and get the entries, get the keys within those entries using Object.keys(...) again.

Is there a more standard / faster / better way to do this?

I'm worried that since our dataset is actually pretty big, it might not perform very well, especially if eventually we had to do the same thing for something, say three levels down or more.

Any help would would be appreciated!

CustardBun
  • 3,457
  • 8
  • 39
  • 65
  • 1
    You got the core of it, see this very related Q&A https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json .. Another way I can imagine (and mentioned in the linked question) would be to leverage builtin JSON serialization functions. for that, see also https://stackoverflow.com/questions/16466220/limit-json-stringification-depth – Pac0 Jul 29 '20 at 22:01
  • 1
    For the optimization part, well, you'll have to see what you can manage. Are you able to get partial data on different dimensions (divide your top-level array into chunks? or load all top-level data, then "on-demand" go deeper) ? – Pac0 Jul 29 '20 at 22:02
  • This feels like an XY problem. ["That is, you are trying to solve problem X, and you think solution Y would work, but instead of asking about X when you run into trouble, you ask about Y."](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Travis J Jul 29 '20 at 22:16

3 Answers3

1

I doubt there is something better than this, at least in my knowdlege, which is exactly what you suggested:

Object.keys(myThing).reduce((acc,val)=> [...acc, ...Object.keys(myThing[val])], []);
Talmacel Marian Silviu
  • 1,596
  • 2
  • 6
  • 14
0

I created a function for level x. Getting for all keys recursive for the obj.key the result. Going this way on until there is only depth 1 left, than return all the keys.

Remark: For don't getting double keynames like someKeySecondLevel1 in the example I push a new key to the result only if this key not exists in the result-part which is created till now. Only in the last depth this should not be necessary because an object can't posess the same key multiple.

myThing = {
   'someKeyTopLevel1': {
      'someKeySecondLevel1': [],
      'someKeySecondLevel5': []
   },
   'someKeyTopLevel2': {
      'someKeySecondLevel1': [],
      'someKeySecondLevel2': [],
      'someKeySecondLevel3': []
   }
};

function getDeepKey(obj, deep) {
    if (deep == 1)
        return Object.keys(obj);

    deep--;
    let result = [];
    for (var key in obj) {
       let answer = getDeepKey(obj[key],deep);
       answer.forEach(element => {
        if (result.indexOf(element) === -1)
            result.push(element);
       });
    }
    return result;
}

console.log(getDeepKey(myThing, 2));
Sascha
  • 4,576
  • 3
  • 13
  • 34
0
     myThing = {
       'someKeyTopLevel1': {
          'someKeySecondLevel1': [],
          'someKeySecondLevel5': []
       },
       'someKeyTopLevel2': {
          'someKeySecondLevel1': [],
          'someKeySecondLevel2': [],
          'someKeySecondLevel3': []
       }
    };

   for (var l1Key in myThing) {  
      for (var l2Key in myThing[l1Key ]) {
        console.log(l2Key);
      }
    }
BCM
  • 665
  • 5
  • 20