我有一个对象,它的值是其他对象
我想通过其父键将对象的名称提取为字符串,例如(input, expected output) = ('home', 'someObj), ('anotherOne', 'anotherObj')
.
到目前为止,我尝试了以下操作,但它返回 [object Object]。
我也尝试过,JSON.stringify(data[key].key1)
但没有返回我想要的。有没有办法实现这一目标?
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'
Javascript 对象没有名称。您必须小心自己,并且可能会添加一个“名称”字段,您可以在其中在对象实例上设置标识符。
您希望对象的“名称”可以像您分配给它的变量的名称一样。但事实并非如此。对象实例和变量名之间零连接。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句