Wednesday, 11 September 2013

can this iteration be done with some nicer method?

can this iteration be done with some nicer method?

Here is my initial solution to an study question regarding filling an
object with items from an array... But it seems like there should be a
nicer solution. I read where "for... in" is not the proper way, but isn't
there some nicer method I'm forgetting?
var doubler = function(arr) // fills object with numbers and their doubled
value
{
var my_obj = {};
for(var i=0; i<arr.length; i++)
{
my_obj[arr[i]] = arr[i]*2;
}
console.log(my_obj);
}
doubler([5,7,2,3,4,7])

No comments:

Post a Comment