Reputation: 101
I try to use this expression to do it.
_.times(3, _.random(1, 10))
but return this:
[undefined, undefined, undefined]
my expect result like this:
[3, 7, 5]
Upvotes: 4
Views: 1483
Reputation: 762
_.map(new Array(3),() => _.random(1,10))
This works because the Array constructor considers an integer as the sole argument as an arrayLength.
Since we now have an array with a length of 3, we can use map
with it to return a new array of the same length, but with each value generated from the return value of a function.
Upvotes: 0
Reputation: 386660
You could wrap the random call in a callback, because _.times
expects an iteratee. That is a number your case and that results to undefined
.
console.log(_.random(1, 10)); // number, not a function
console.log(_.times(3, 42)); // call with a number
console.log(_.times(3, i => _.random(1, 10))); // call with function
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
Upvotes: 2
Reputation: 232
Here is a quick way to do it for 3 values -
var nums[3];
for(var x = 0;x<3;x++){num[x] = random(1,10);}
Upvotes: 0