[英]Add Variable to Object Literal
I'm trying to add a variable to an object I'm creating, but when I log the object id
appears simply as id
, instead of my ref.key()
value. 我正在尝试向要创建的对象添加变量,但是当我登录时,对象id
只是显示为id
,而不是我的ref.key()
值。
var id = ref.key();
var newItem = {
'provider1': {
'services': {
id: true
}
}
};
console.log(newItem);
Any idea what I'm doing wrong? 知道我在做什么错吗? Any help is appreciated. 任何帮助表示赞赏。 Thanks in advance! 提前致谢!
You should use bracket notation: 您应该使用方括号表示法:
var id = ref.key();
var newItem = {
'provider1': {
'services': {}
}
};
newItem.provider1.services[id] = true;
console.log(newItem);
var id = ref.key();
and 和
'services': {
id: true
}
Have absolutely nothing to do with each other. 彼此之间毫无关系。 One is a variable
while the other is an object key name
that you are setting to true
. 一个是variable
,另一个是要设置为true
的object key name
。
'services': {
id: id // <-- this id has your ref.key() value
}
Would make more sense. 会更有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.