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.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With