2014-10-11 63 views
0

我有這種類型的數組:dynamiclly在javascript中構建對象鍵

var someArray = ['9213','9234'];

我要做到以下幾點有以下結果:

var obj = { 
    9213:true, 
    9234:true 
} 

我怎樣才能做到這一點?是這樣的:

obj = []; 
_.each(someArray, function(currentNum,i){ 
    obj.push(); //here i should do something 
}) 
+2

如果你想一個對象('{}')你爲什麼初始化數組('[]')? – 2014-10-11 12:34:15

回答

2

像這樣:

obj = {}; // {} means object and [] means array 
_.each(someArray, function(currentNum,i){ 
    obj[ currentNum ] = true; 
    //by using [ currentNum ] you will create object property name as "9213" for example. 
})