-1

I have an object where its values are other objects

I would like to extract the name of the object by its parent key as a string, e.g., (input, expected output) = ('home', 'someObj), ('anotherOne', 'anotherObj').

So far I tried the following, but it returns [object Object].

I also tried JSON.stringify(data[key].key1) but does not return what I want. Is there a way to achieve this?

const someObj = {
  something: 'la'
}
const anotherObj = {
  something: 'be'
}

const data = {
  'home': {
    key1: someObj
  },
  'anotherOne': {
    key1: anotherObj
  }
}
console.log(data)


const key = 'home'
const output = `${data[key].key1}`

console.log(output) // expected output: 'someObj'
mplungjan
  • 169,008
  • 28
  • 173
  • 236
xerroxcopy
  • 17
  • 3
  • No, that's not possible. – Andreas May 27 '20 at 06:48
  • 4
    `someObj` is the name of the original variable, not the name of an object or anything else. That information isn't stored anywhere. This is probably an [XY problem](http://xyproblem.info/) so you might want to ask how to solve the actual problem instead. – Guy Incognito May 27 '20 at 06:49
  • I think this might have a solution to your problem: https://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript – NewPythonUser May 27 '20 at 06:51
  • `Object.keys(data).flatMap(k=>[k, Object.values(data[k]])).join(',')` – gorak May 27 '20 at 06:55

2 Answers2

1

Javascript objects don't have names. You'd have to take care yourself and probably add a 'name' field where you set an identifier on an object instance.

You hope, that the 'name' of the object could be like the name of the variable that you assign it to. But that's not true. There is zero connection between the object instance and the variable name.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
0

The problem is that you are assigning the variable value and not its name. Because JS compiler will not keep track of the variable name that it used to assign values to so,

const someObj = {
  something: 'la'
}
const anotherObj = {
  something: 'be'
}

const data = {
  'home': {
    key1: someObj
  },
  'anotherOne': {
    key1: anotherObj
  }
}

JS compiles it as

const data = {
  'home': {
    key1: {
     something: 'la' // Doesn't keep track of the original variable name used to assign value.
    }

  },
  'anotherOne': {
    key1: {
     something: 'be'
    }
  }
}

And when you fetch data[key].key1. it returns {something: 'la'} which is an object and when you use string template literal `${data[key].key1}`. It typecast it into string as

`${{}}` // Outputs "[object Object]"

So, you need to store the variable names as string

const someObj = {
  something: 'la'
}
const anotherObj = {
  something: 'be'
}

const data = {
  'home': {
    key1: 'someObj'
  },
  'anotherOne': {
    key1: 'anotherObj'
  }
}
console.log(data)


const key = 'home'
const output = `${data[key].key1}`

console.log(output) // output: 'someObj'

This code will work

You need to set the variable name as string.