在javascript中动态构建对象键

时间:2014-10-11 12:32:05

标签: javascript

我有这种阵列:

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

我想做以下结果有以下结果:

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

我怎样才能做到这一点?类似的东西:

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

1 个答案:

答案 0 :(得分: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.
})