The arrTake() method returns a new array with the first n elements from the source array. If n is greater than the array length, returns all elements. If n is negative or 0, returns empty array.
Identifies the base type of array elements
The array or array-like object to take from
The number of elements to take from the beginning
A new array containing the first n elements
0.14.0
arrTake([1, 2, 3, 4, 5], 3); // [1, 2, 3]arrTake(["a", "b", "c"], 2); // ["a", "b"]arrTake([1, 2], 5); // [1, 2]arrTake([1, 2, 3], 0); // []arrTake([1, 2, 3], -1); // []// Array-like objectsarrTake({ length: 4, 0: 1, 1: 2, 2: 3, 3: 4 }, 2); // [1, 2] Copy
arrTake([1, 2, 3, 4, 5], 3); // [1, 2, 3]arrTake(["a", "b", "c"], 2); // ["a", "b"]arrTake([1, 2], 5); // [1, 2]arrTake([1, 2, 3], 0); // []arrTake([1, 2, 3], -1); // []// Array-like objectsarrTake({ length: 4, 0: 1, 1: 2, 2: 3, 3: 4 }, 2); // [1, 2]
The arrTake() method returns a new array with the first n elements from the source array. If n is greater than the array length, returns all elements. If n is negative or 0, returns empty array.