Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic object literal in javascript?

Is it possible to creat an object literal on the fly? Like this:

var arr = [ 'one', 'two', 'three' ]; 

var literal = {}; 

for(var i=0;i<arr.length;i++)
{
   // some literal push method here! 

  /*  literal = {
        one : "", 
        two : "",
        three : ""
    }  */ 
}

Thus I want the result to be like this:

 literal = {
        one : "", 
        two : "",
        three : ""
    } 
like image 462
marko Avatar asked Jan 04 '10 10:01

marko


2 Answers

for ( var i = 0, l = arr.length; i < l; ++i ) {
    literal[arr[i]] = "something";
}

I also took the liberty of optimising your loop :)

like image 171
James Avatar answered Oct 20 '22 16:10

James


Use this in your loop:

literal[arr[i]] = "";
like image 35
Alsciende Avatar answered Oct 20 '22 14:10

Alsciende