2016-04-13 26 views
1

是否有任何選項,因爲我可以在對象聲明的左側使用變量?就像這樣:在對象聲明的左側使用變量

var col = 'col'; 
var gridDataVK4000 = { 
    items : [] 
}; 

for (var i = 0; i <= 14; i++) { 
    col += col + i; 
    // we now push to the item property 
    gridDataVK4000.items.push({ 
     col : i, 
    }); 
} 

因爲我的例子不起作用。 :(

+0

什麼是您預期的結果? –

+1

做這個col增量的原因是什麼?我認爲那不清楚 –

回答

1

你需要聲明它用方括號花括號外面,否則你分配的關鍵"col",從字面上看,你的對象。

var result = {}; 
result[col] = i; 

gridDataVK4000.items.push(result);