0
# lodash.take
1
2
The `_.take` function creates a slice of an array with n elements taken from the beginning. It's part of the lodash utility library, providing a safe and consistent way to extract the first few elements of an array with built-in null handling and edge case management.
3
4
## Package Information
5
6
- **Package Name**: lodash
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash`
10
11
## Core Imports
12
13
```javascript
14
import _ from "lodash";
15
```
16
17
For ES6 named imports:
18
19
```javascript
20
import { take } from "lodash";
21
```
22
23
For CommonJS:
24
25
```javascript
26
const _ = require("lodash");
27
```
28
29
Or specific function import:
30
31
```javascript
32
const { take } = require("lodash");
33
```
34
35
## Basic Usage
36
37
```javascript
38
import { take } from "lodash";
39
40
// Take first element (default behavior)
41
const firstItem = take([1, 2, 3, 4, 5]);
42
// => [1]
43
44
// Take first 3 elements
45
const firstThree = take([1, 2, 3, 4, 5], 3);
46
// => [1, 2, 3]
47
48
// Safe handling of edge cases
49
const empty = take([], 5); // => []
50
const none = take(null, 2); // => []
51
const zero = take([1, 2, 3], 0); // => []
52
```
53
54
## Capabilities
55
56
### Array Slicing
57
58
Creates a slice of array with n elements taken from the beginning.
59
60
```javascript { .api }
61
/**
62
* Creates a slice of `array` with `n` elements taken from the beginning.
63
* @param {Array} array - The array to query
64
* @param {number} [n=1] - The number of elements to take
65
* @returns {Array} Returns the slice of `array`
66
*/
67
function take(array, n);
68
```
69
70
**Parameters:**
71
- `array` (Array): The array to query. Can be null/undefined (returns empty array)
72
- `n` (number, optional): The number of elements to take. Defaults to 1 if not provided
73
74
**Returns:**
75
- (Array): Returns a new array containing the slice of the input array
76
77
**Behavior:**
78
- If `array` is null, undefined, or empty, returns `[]`
79
- If `n` is null or undefined, defaults to 1
80
- If `n` is 0 or negative, returns `[]`
81
- If `n` exceeds array length, returns all available elements
82
- Always returns a new array (does not mutate original)
83
84
**Usage Examples:**
85
86
```javascript
87
import { take } from "lodash";
88
89
// Basic usage
90
take([1, 2, 3]);
91
// => [1]
92
93
take([1, 2, 3], 2);
94
// => [1, 2]
95
96
take([1, 2, 3], 5);
97
// => [1, 2, 3]
98
99
take([1, 2, 3], 0);
100
// => []
101
102
// Edge cases
103
take(null); // => []
104
take(undefined); // => []
105
take([]); // => []
106
take([1, 2, 3], -1); // => []
107
108
// String arrays
109
take(['a', 'b', 'c', 'd'], 2);
110
// => ['a', 'b']
111
112
// Object arrays
113
const users = [
114
{ name: 'Alice', age: 25 },
115
{ name: 'Bob', age: 30 },
116
{ name: 'Charlie', age: 35 }
117
];
118
take(users, 2);
119
// => [{ name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }]
120
121
// Mixed type arrays
122
take([1, 'hello', true, null, { key: 'value' }], 3);
123
// => [1, 'hello', true]
124
```
125
126
## Common Use Cases
127
128
### Pagination
129
```javascript
130
import { take } from "lodash";
131
132
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
133
const pageSize = 3;
134
135
// Get first page
136
const firstPage = take(items, pageSize);
137
// => [1, 2, 3]
138
```
139
140
### Data Sampling
141
```javascript
142
import { take } from "lodash";
143
144
const dataSet = [/* large array of data */];
145
const sample = take(dataSet, 100); // Get first 100 items for preview
146
```
147
148
### Safe Array Truncation
149
```javascript
150
import { take } from "lodash";
151
152
function getTopResults(results, limit = 10) {
153
// Safely limit results without worrying about null/undefined
154
return take(results, limit);
155
}
156
```
157
158
### Integration with Method Chaining
159
```javascript
160
import _ from "lodash";
161
162
const result = _([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
163
.filter(n => n % 2 === 0) // Get even numbers: [2, 4, 6, 8, 10]
164
.take(3) // Take first 3: [2, 4, 6]
165
.value();
166
// => [2, 4, 6]
167
```