Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing Underscore.js' range()

Using range() in Underscore I can make something like this:

_.range(10);
>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Can I somehow modify/use that to create a result such as this:

solution();
>> {0: true, 1: true, 2: true, 3: true}

The solution may also include jQuery.

like image 583
js999 Avatar asked Feb 18 '23 21:02

js999


1 Answers

Yes.

var range = _.range(10);
_.map(range, function() { return true; });

jsFiddle.

If you must have an object (the former returns an array), run this on the result...

_.extend({}, range);

jsFiddle.

It's worth mentioning, if you didn't have Underscore or wanted to use jQuery, there are equivalents $.map() and $.extend().

like image 117
alex Avatar answered Feb 27 '23 07:02

alex