0
# Object Functions
1
2
Object manipulation utilities for merging, transforming, and analyzing object properties. These functions provide comprehensive object handling capabilities for data processing and state management.
3
4
## Capabilities
5
6
### Assign
7
8
Assigns own enumerable string keyed properties of source objects to the destination object.
9
10
```javascript { .api }
11
/**
12
* Assigns own enumerable string keyed properties of source objects to the
13
* destination object. Source objects are applied from left to right.
14
* Subsequent sources overwrite property assignments of previous sources.
15
*
16
* **Note:** This method mutates `object` and is loosely based on
17
* [`Object.assign`](https://mdn.io/Object/assign).
18
*
19
* @param {Object} object The destination object.
20
* @param {...Object} [sources] The source objects.
21
* @returns {Object} Returns `object`.
22
*/
23
function assign(object, ...sources);
24
```
25
26
**Usage Examples:**
27
28
```javascript
29
var assign = require('lodash.assign');
30
31
assign({ a: 1 }, { b: 2 }, { c: 3 });
32
// => { a: 1, b: 2, c: 3 }
33
34
var users = { name: 'alice' };
35
assign(users, { age: 25 }, { active: true });
36
// => { name: 'alice', age: 25, active: true }
37
```
38
39
### Clone
40
41
Creates a shallow clone of value.
42
43
```javascript { .api }
44
/**
45
* Creates a shallow clone of `value`.
46
*
47
* **Note:** This method is loosely based on the
48
* [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm)
49
* and supports cloning arrays, array buffers, booleans, date objects, maps,
50
* numbers, `Object` objects, regexes, sets, strings, symbols, and typed arrays.
51
*
52
* @param {*} value The value to clone.
53
* @returns {*} Returns the cloned value.
54
*/
55
function clone(value);
56
```
57
58
**Usage Examples:**
59
60
```javascript
61
var clone = require('lodash.clone');
62
63
var objects = [{ a: 1 }, { b: 2 }];
64
var shallow = clone(objects);
65
66
console.log(shallow[0] === objects[0]);
67
// => true (shallow clone - references preserved)
68
```
69
70
### CloneDeep
71
72
Creates a deep clone of value.
73
74
```javascript { .api }
75
/**
76
* This method is like `_.clone` except that it recursively clones `value`.
77
*
78
* @param {*} value The value to recursively clone.
79
* @returns {*} Returns the deep cloned value.
80
*/
81
function cloneDeep(value);
82
```
83
84
**Usage Examples:**
85
86
```javascript
87
var cloneDeep = require('lodash.clonedeep');
88
89
var objects = [{ a: 1 }, { b: 2 }];
90
var deep = cloneDeep(objects);
91
92
console.log(deep[0] === objects[0]);
93
// => false (deep clone - new objects created)
94
```
95
96
### Merge
97
98
Recursively merges own and inherited enumerable string keyed properties of source objects into the destination object.
99
100
```javascript { .api }
101
/**
102
* This method is like `_.assign` except that it recursively merges own and
103
* inherited enumerable string keyed properties of source objects into the
104
* destination object. Source properties that resolve to `undefined` are skipped
105
* if a destination value exists.
106
*
107
* **Note:** This method mutates `object`.
108
*
109
* @param {Object} object The destination object.
110
* @param {...Object} [sources] The source objects.
111
* @returns {Object} Returns `object`.
112
*/
113
function merge(object, ...sources);
114
```
115
116
**Usage Examples:**
117
118
```javascript
119
var merge = require('lodash.merge');
120
121
var object = {
122
a: [{ b: 2 }, { d: 4 }]
123
};
124
125
var other = {
126
a: [{ c: 3 }, { e: 5 }]
127
};
128
129
merge(object, other);
130
// => { a: [{ b: 2, c: 3 }, { d: 4, e: 5 }] }
131
```
132
133
### Keys
134
135
Creates an array of the own enumerable property names of object.
136
137
```javascript { .api }
138
/**
139
* Creates an array of the own enumerable property names of `object`.
140
*
141
* **Note:** Non-object values are coerced to objects. See the
142
* [ES spec](http://ecma-international.org/ecma-262/6.0/#sec-object.keys)
143
* for more details.
144
*
145
* @param {Object} object The object to query.
146
* @returns {Array} Returns the array of property names.
147
*/
148
function keys(object);
149
```
150
151
**Usage Examples:**
152
153
```javascript
154
var keys = require('lodash.keys');
155
156
function Foo() {
157
this.a = 1;
158
this.b = 2;
159
}
160
161
Foo.prototype.c = 3;
162
163
keys(new Foo);
164
// => ['a', 'b'] (iteration order is not guaranteed)
165
166
keys('hi');
167
// => ['0', '1']
168
```
169
170
### Values
171
172
Creates an array of the own enumerable string keyed property values of object.
173
174
```javascript { .api }
175
/**
176
* Creates an array of the own enumerable string keyed property values of `object`.
177
*
178
* **Note:** Non-object values are coerced to objects.
179
*
180
* @param {Object} object The object to query.
181
* @returns {Array} Returns the array of property values.
182
*/
183
function values(object);
184
```
185
186
**Usage Examples:**
187
188
```javascript
189
var values = require('lodash.values');
190
191
function Foo() {
192
this.a = 1;
193
this.b = 2;
194
}
195
196
Foo.prototype.c = 3;
197
198
values(new Foo);
199
// => [1, 2] (iteration order is not guaranteed)
200
201
values('hi');
202
// => ['h', 'i']
203
```
204
205
### Pick
206
207
Creates an object composed of the picked object properties.
208
209
```javascript { .api }
210
/**
211
* Creates an object composed of the picked `object` properties.
212
*
213
* @param {Object} object The source object.
214
* @param {...(string|string[])} [props] The property identifiers to pick.
215
* @returns {Object} Returns the new object.
216
*/
217
function pick(object, ...props);
218
```
219
220
**Usage Examples:**
221
222
```javascript
223
var pick = require('lodash.pick');
224
225
var object = { a: 1, b: '2', c: 3 };
226
227
pick(object, ['a', 'c']);
228
// => { a: 1, c: 3 }
229
230
pick(object, 'a', 'c');
231
// => { a: 1, c: 3 }
232
```
233
234
### Omit
235
236
Creates an object composed of the own and inherited enumerable properties of object that are not omitted.
237
238
```javascript { .api }
239
/**
240
* The opposite of `_.pick`; this method creates an object composed of the
241
* own and inherited enumerable properties of `object` that are not omitted.
242
*
243
* @param {Object} object The source object.
244
* @param {...(string|string[])} [props] The property identifiers to omit.
245
* @returns {Object} Returns the new object.
246
*/
247
function omit(object, ...props);
248
```
249
250
**Usage Examples:**
251
252
```javascript
253
var omit = require('lodash.omit');
254
255
var object = { a: 1, b: '2', c: 3 };
256
257
omit(object, ['a', 'c']);
258
// => { b: '2' }
259
260
omit(object, 'a', 'c');
261
// => { b: '2' }
262
```
263
264
### Has
265
266
Checks if path is a direct property of object.
267
268
```javascript { .api }
269
/**
270
* Checks if `path` is a direct property of `object`.
271
*
272
* @param {Object} object The object to query.
273
* @param {Array|string} path The path to check.
274
* @returns {boolean} Returns `true` if `path` exists, else `false`.
275
*/
276
function has(object, path);
277
```
278
279
**Usage Examples:**
280
281
```javascript
282
var has = require('lodash.has');
283
284
var object = { a: { b: 2 } };
285
286
has(object, 'a');
287
// => true
288
289
has(object, 'a.b');
290
// => true
291
292
has(object, ['a', 'b']);
293
// => true
294
```
295
296
### Defaults
297
298
Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to undefined.
299
300
```javascript { .api }
301
/**
302
* Assigns own and inherited enumerable string keyed properties of source
303
* objects to the destination object for all destination properties that
304
* resolve to `undefined`. Source objects are applied from left to right.
305
* Once a property is set, additional values of the same property are ignored.
306
*
307
* **Note:** This method mutates `object`.
308
*
309
* @param {Object} object The destination object.
310
* @param {...Object} [sources] The source objects.
311
* @returns {Object} Returns `object`.
312
*/
313
function defaults(object, ...sources);
314
```
315
316
**Usage Examples:**
317
318
```javascript
319
var defaults = require('lodash.defaults');
320
321
defaults({ a: 1 }, { b: 2 }, { a: 3 });
322
// => { a: 1, b: 2 }
323
```
324
325
### Invert
326
327
Creates an object composed of the inverted keys and values of object.
328
329
```javascript { .api }
330
/**
331
* Creates an object composed of the inverted keys and values of `object`.
332
* If `object` contains duplicate values, subsequent values overwrite property
333
* assignments of previous values unless `multiValue` is `true`.
334
*
335
* @param {Object} object The object to invert.
336
* @param {boolean} [multiValue] Allow multiple values per key.
337
* @returns {Object} Returns the new inverted object.
338
*/
339
function invert(object, multiValue);
340
```
341
342
**Usage Examples:**
343
344
```javascript
345
var invert = require('lodash.invert');
346
347
var object = { a: 1, b: 2, c: 1 };
348
349
invert(object);
350
// => { '1': 'c', '2': 'b' }
351
352
// Handle multiple values
353
invert(object, true);
354
// => { '1': ['a', 'c'], '2': ['b'] }
355
```
356
357
## Additional Functions
358
359
The object category also includes: `create`, `findKey`, `findLastKey`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`, `functions`, `keysIn`, `mapValues`, `pairs`, `result`, `transform`, `valuesIn`
360
361
**Package Installation:**
362
363
```bash
364
npm install lodash.assign lodash.clone lodash.clonedeep lodash.merge
365
npm install lodash.keys lodash.values lodash.pick lodash.omit
366
npm install lodash.has lodash.defaults lodash.invert
367
```