2017-05-16 38 views
0

我有一個對象「配置」和ID「ID」, 我想創建對象的數組具有下列結構:角/打字稿創建的對象與動態密鑰

[ 
    "id" : { 
      "config1: ... 
      "config2: ... 
      "config3: ... 

     } 
    "id2" : { 
      "config1: ... 
      "config2: ... 
      "config3: ... 

     } 

] 

我試圖下面的代碼:

garage.push({this.id : this.config }); 

但它會導致編譯錯誤,如:

ERROR in component.ts (168,51): An object literal cannot have multiple properties with the same name in strict mode. 

我怎麼能做這個工作?

+1

'garage'是如何聲明的?它的類型是什麼? –

+0

let garage = []; – Moshe

+1

_「編譯錯誤」_?可以將這些添加到您的問題? – evolutionxbox

回答

2

你可以嘗試以下,

let _temp = {}; 
_temp[this.id] = this.config; 
garage.push(_temp); 

希望這有助於!

+0

解決了語法問題,但OP仍然會遇到他們的「編譯」錯誤。 – evolutionxbox