The accepted answer is only suitable for this question, but it should be noted that, as indicated, it will only work for a number of incremental integers starting from zero, since it actually uses the index, not the value of the element.
Here is another one of many possible solutions. When I need to turn an array into a hash search, I do this:
var range = _.range(10); var hash = _.object(range, range.map(_.const(true)));
This will take into account the actual values ββin your source array, which can be numbers or strings in any order.
I do not particularly recommend this, but if you can be sure that your original range does not contain zero, you can simplify it further, since all values ββwill be true:
var hash = _.object(range, range);
source share