totothegreat
totothegreat

Reputation: 1643

dynamiclly build object keys in javascript

I have this kind of array:

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

I want to do the following have the following result:

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

How can i achieve this? something like:

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

Upvotes: 0

Views: 29

Answers (1)

antyrat
antyrat

Reputation: 27765

Like this:

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.
})

Upvotes: 2

Related Questions