Returns the nth element of an array.
Array.prototype.slice()
to get an array containing the nth element at the first place.undefined
.n
, to get the first element of the array.const nthElement = (arr, n = 0) =>
(n === -1 ? arr.slice(n) : arr.slice(n, n + 1))[0];
nthElement(['a', 'b', 'c'], 1); // 'b'
nthElement(['a', 'b', 'b'], -3); // 'a'
JavaScript, Array
Returns every nth
element in an array.
JavaScript, Array
Creates an array of elements, ungrouping the elements in an array produced by zip and applying the provided function.
JavaScript, Array
Creates an array of arrays, ungrouping the elements in an array produced by zip.