Gets the first n
elements of an array.
Array.prototype.slice()
with a start value of 0
and an end value of n
to get the first n
elements of arr
.const firstN = (arr, n) => arr.slice(0, n);
firstN(['a', 'b', 'c', 'd'], 2); // ['a', 'b']
JavaScript, Array
Gets n
random elements at unique keys from an array up to the size of the array.
JavaScript, Array
Gets a random element from an array, using the provided weights
as the probabilities for each element.
JavaScript, Array
Finds the first n
elements for which the provided function returns a truthy value.