0

I have an object:

var Top = {
    'A':{},
    'b':{
        '1':'someText',
        '2':'someMoreText'},
    'C':{
        '3':'evenMoreText',
        '4':'thisIsGettingRedundant'},
    'D':'thisOneIsDifferent'}

I am looking for a way to access the name of my objects, like Top[b].objectName would return 'Top' as a string, I am doing this inside some nested for...in loops, like this:

for(thing in Top){
    for(piece in Top[thing]){
        console.log('Grabbing ' + Top[thing][piece] + ' from ' + MY_OBJECT_NAME_THAT_SHOULD_BE_TOP);
    }
}

I suppose I could add a tag to each object for their name (IE. changing A from {} to 'Top'), but that seems redundant to me.

-Edit:

is there a way to log my second level object names IE. A, B, C? as they SHOULD be logged as data and not code

Spencer Cornwall
  • 289
  • 2
  • 14
  • 1
    This seems like something that would break when code gets minified. Check out [variable name as string in javascript](https://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript) – byxor Jun 26 '17 at 16:22
  • 1
    There is [this](https://stackoverflow.com/a/24100885/7852370) approach, where you look at all the properties in global `Window` and find the one matching your object. – Karl Reid Jun 26 '17 at 16:24
  • 2
    There is no way to do this as an object could be referenced by many variables, eg `a = b = c = {}`, there is no mechanism for the interpreter to know which variable you are meaning to name. – Patrick Evans Jun 26 '17 at 16:27
  • 1
    I wouldn't think it's possible because Top is just a variable which points to the object in memory, but so could multiple variables. e.g. var Top, Bottom = {}. So there's not a 1 to 1 relationship between the variable and the object. – PeteG Jun 26 '17 at 16:27
  • 1
    Also I disagree that having a field for the 'name' of your object is redundant - it's a very standard practice. Code and data should be separate. – Karl Reid Jun 26 '17 at 16:27
  • well, I'll add a name tag, and keep looking, maybe break some more stuff, it'll all be good – Spencer Cornwall Jun 26 '17 at 16:32
  • var name="Top"; for(key in window[name]){ console.log("parent is "+name);} – Jonas Wilms Jun 26 '17 at 16:34
  • Concerning your edit: console.log(thing,piece); – Jonas Wilms Jun 26 '17 at 16:35
  • @Jonasw That gives me [object object] which isn't all that useful – Spencer Cornwall Jun 26 '17 at 16:43
  • @spencer cornwall what?? I think you have not tried my code... – Jonas Wilms Jun 26 '17 at 16:46
  • @Jonasw My apologies, that is a comma... – Spencer Cornwall Jun 26 '17 at 16:48
  • See [my answer](https://stackoverflow.com/a/19329402/266535) for how I solve this type of problem. – styfle Jun 26 '17 at 17:03
  • Possible duplicate of [Determine original name of variable after its passed to a function](https://stackoverflow.com/questions/3404057/determine-original-name-of-variable-after-its-passed-to-a-function) – styfle Jun 26 '17 at 17:05

1 Answers1

0
const Top = {
  'A': {},
  'b': {
    '1': 'someText',
    '2': 'someMoreText'
  },
  'C': {
    '3': 'evenMoreText',
    '4': 'thisIsGettingRedundant'
  },
  'D': 'thisOneIsDifferent'
};

To achieve this, use a for-in-loop to iterate over all the keys @ the top level. If the key points to an object and is not equal to null (defined as an object in JavaScript), perform another for-in-loop to log each item into the console. Should the top level key point to an empty object, log empty curvy brackets to the console. On the other hand, if the top-level key point to a non-object, log that to the console.

for(let key in Top) {
  if(typeof Top[key] === 'object' && Top[key] !== null) {
    for(let item in Top[key]) {
      console.log(`Grabbing ${Top[key][item]} from '${item}' within '${key}' in Top.`);
    }

    if(Object.keys(Top[key]).length === 0) {
      console.log(`Grabbing {} from '${key}' in Top.`);
    }
  } else {
    console.log(`Grabbing ${Top[key]} from '${key}' in Top.`);
  }
}
elqueso101
  • 84
  • 3