2024-04-25 14:56:13 +03:00
|
|
|
# take
|
|
|
|
|
|
|
|
Returns a new array containing the first `count` elements from the input array `arr`.
|
|
|
|
|
|
|
|
If `count` is greater than the length of `arr`, the entire array is returned.
|
|
|
|
|
|
|
|
## Signature
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
function take<T>(arr: T[], count: number): T[];
|
|
|
|
```
|
|
|
|
|
2024-06-04 11:19:26 +03:00
|
|
|
### Parameters
|
2024-04-25 14:56:13 +03:00
|
|
|
|
|
|
|
- `arr` (`T[]`): The array to take elements from.
|
|
|
|
- `count` (`number`): The number of elements to take.
|
|
|
|
|
|
|
|
### Returns
|
|
|
|
|
|
|
|
(`T[]`): A new array containing the first `count` elements from `arr`.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
// Returns [1, 2, 3]
|
|
|
|
take([1, 2, 3, 4, 5], 3);
|
|
|
|
|
|
|
|
// Returns ['a', 'b']
|
|
|
|
take(['a', 'b', 'c'], 2);
|
|
|
|
|
|
|
|
// Returns [1, 2, 3]
|
|
|
|
take([1, 2, 3], 5);
|
2024-06-04 11:19:26 +03:00
|
|
|
```
|