[英]Angular/TypeScript create object with dynamic key
I have an object "config" and an id "id", I would like to create an array of objects that has the following structure: 我有一个对象“ config”和一个id“ id”,我想创建一个具有以下结构的对象数组:
[
"id" : {
"config1: ...
"config2: ...
"config3: ...
}
"id2" : {
"config1: ...
"config2: ...
"config3: ...
}
]
I tried the following code: 我尝试了以下代码:
garage.push( {this.id : this.config });
but it causes compiling errors such as: 但是会导致编译错误,例如:
ERROR in component.ts (168,51): An object literal cannot have multiple properties with the same name in strict mode.
How can I make this work? 我该如何进行这项工作?
you may try below, 您可以在下面尝试,
let _temp = {};
_temp[this.id] = this.config;
garage.push(_temp);
Hope this helps!! 希望这可以帮助!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.