_.assign(object, [sources], [customizer], [thisArg])

Assigns own enumerable properties of source object(s) to the destination object. Subsequent sources overwrite property assignments of previous sources. If customizer is provided it’s invoked to produce the assigned values. The customizer is bound to thisArg and invoked with five arguments:
(objectValue, sourceValue, key, object, source).

Note: This method mutates object and is based on Object.assign.

Aliases

_.extend

Arguments

  1. object (Object): The destination object.
  2. [sources] (…Object): The source objects.
  3. [customizer] (Function): The function to customize assigned values.
  4. [thisArg] (*): The this binding of customizer.

Returns

(Object): Returns object.

Example

  1. _.assign({ 'user': 'barney' }, { 'age': 40 }, { 'user': 'fred' });
  2. // => { 'user': 'fred', 'age': 40 }
  3. // using a customizer callback
  4. var defaults = _.partialRight(_.assign, function(value, other) {
  5. return _.isUndefined(value) ? other : value;
  6. });
  7. defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' });
  8. // => { 'user': 'barney', 'age': 36 }

_.create(prototype, [properties])

Creates an object that inherits from the given prototype object. If a properties object is provided its own enumerable properties are assigned to the created object.

Arguments

  1. prototype (Object): The object to inherit from.
  2. [properties] (Object): The properties to assign to the object.

Returns

(Object): Returns the new object.

Example

  1. function Shape() {
  2. this.x = 0;
  3. this.y = 0;
  4. }
  5. function Circle() {
  6. Shape.call(this);
  7. }
  8. Circle.prototype = _.create(Shape.prototype, {
  9. 'constructor': Circle
  10. });
  11. var circle = new Circle;
  12. circle instanceof Circle;
  13. // => true
  14. circle instanceof Shape;
  15. // => true

_.defaults(object, [sources])

Assigns own enumerable properties of source object(s) to the destination object for all destination properties that resolve to undefined. Once a property is set, additional values of the same property are ignored.

Note: This method mutates object.

Arguments

  1. object (Object): The destination object.
  2. [sources] (…Object): The source objects.

Returns

(Object): Returns object.

Example

  1. _.defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' });
  2. // => { 'user': 'barney', 'age': 36 }

_.defaultsDeep(object, [sources])

This method is like _.defaults except that it recursively assigns default properties.

Note: This method mutates object.

Arguments

  1. object (Object): The destination object.
  2. [sources] (…Object): The source objects.

Returns

(Object): Returns object.

Example

  1. _.defaultsDeep({ 'user': { 'name': 'barney' } }, { 'user': { 'name': 'fred', 'age': 36 } });
  2. // => { 'user': { 'name': 'barney', 'age': 36 } }

.findKey(object, [predicate=.identity], [thisArg])

This method is like _.find except that it returns the key of the first element predicate returns truthy for instead of the element itself.

If a property name is provided for predicate the created _.property style callback returns the property value of the given element.

If a value is also provided for thisArg the created _.matchesProperty style callback returns true for elements that have a matching property value, else false.

If an object is provided for predicate the created _.matches style callback returns true for elements that have the properties of the given object, else false.

Arguments

  1. object (Object): The object to search.
  2. [predicate=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of predicate.

Returns

(string|undefined): Returns the key of the matched element, else undefined.

Example

  1. var users = {
  2. 'barney': { 'age': 36, 'active': true },
  3. 'fred': { 'age': 40, 'active': false },
  4. 'pebbles': { 'age': 1, 'active': true }
  5. };
  6. _.findKey(users, function(chr) {
  7. return chr.age < 40;
  8. });
  9. // => 'barney' (iteration order is not guaranteed)
  10. // using the `_.matches` callback shorthand
  11. _.findKey(users, { 'age': 1, 'active': true });
  12. // => 'pebbles'
  13. // using the `_.matchesProperty` callback shorthand
  14. _.findKey(users, 'active', false);
  15. // => 'fred'
  16. // using the `_.property` callback shorthand
  17. _.findKey(users, 'active');
  18. // => 'barney'

.findLastKey(object, [predicate=.identity], [thisArg])

This method is like _.findKey except that it iterates over elements of a collection in the opposite order.

If a property name is provided for predicate the created _.property style callback returns the property value of the given element.

If a value is also provided for thisArg the created _.matchesProperty style callback returns true for elements that have a matching property value, else false.

If an object is provided for predicate the created _.matches style callback returns true for elements that have the properties of the given object, else false.

Arguments

  1. object (Object): The object to search.
  2. [predicate=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of predicate.

Returns

(string|undefined): Returns the key of the matched element, else undefined.

Example

  1. var users = {
  2. 'barney': { 'age': 36, 'active': true },
  3. 'fred': { 'age': 40, 'active': false },
  4. 'pebbles': { 'age': 1, 'active': true }
  5. };
  6. _.findLastKey(users, function(chr) {
  7. return chr.age < 40;
  8. });
  9. // => returns `pebbles` assuming `_.findKey` returns `barney`
  10. // using the `_.matches` callback shorthand
  11. _.findLastKey(users, { 'age': 36, 'active': true });
  12. // => 'barney'
  13. // using the `_.matchesProperty` callback shorthand
  14. _.findLastKey(users, 'active', false);
  15. // => 'fred'
  16. // using the `_.property` callback shorthand
  17. _.findLastKey(users, 'active');
  18. // => 'pebbles'

.forIn(object, [iteratee=.identity], [thisArg])

Iterates over own and inherited enumerable properties of an object invoking iteratee for each property. The iteratee is bound to thisArg and invoked with three arguments: (value, key, object). Iteratee functions may exit iteration early by explicitly returning false.

Arguments

  1. object (Object): The object to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.
  3. [thisArg] (*): The this binding of iteratee.

Returns

(Object): Returns object.

Example

  1. function Foo() {
  2. this.a = 1;
  3. this.b = 2;
  4. }
  5. Foo.prototype.c = 3;
  6. _.forIn(new Foo, function(value, key) {
  7. console.log(key);
  8. });
  9. // => logs 'a', 'b', and 'c' (iteration order is not guaranteed)

.forInRight(object, [iteratee=.identity], [thisArg])

This method is like _.forIn except that it iterates over properties of object in the opposite order.

Arguments

  1. object (Object): The object to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.
  3. [thisArg] (*): The this binding of iteratee.

Returns

(Object): Returns object.

Example

  1. function Foo() {
  2. this.a = 1;
  3. this.b = 2;
  4. }
  5. Foo.prototype.c = 3;
  6. _.forInRight(new Foo, function(value, key) {
  7. console.log(key);
  8. });
  9. // => logs 'c', 'b', and 'a' assuming `_.forIn ` logs 'a', 'b', and 'c'

.forOwn(object, [iteratee=.identity], [thisArg])

Iterates over own enumerable properties of an object invoking iteratee for each property. The iteratee is bound to thisArg and invoked with three arguments: (value, key, object). Iteratee functions may exit iteration early by explicitly returning false.

Arguments

  1. object (Object): The object to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.
  3. [thisArg] (*): The this binding of iteratee.

Returns

(Object): Returns object.

Example

  1. function Foo() {
  2. this.a = 1;
  3. this.b = 2;
  4. }
  5. Foo.prototype.c = 3;
  6. _.forOwn(new Foo, function(value, key) {
  7. console.log(key);
  8. });
  9. // => logs 'a' and 'b' (iteration order is not guaranteed)

.forOwnRight(object, [iteratee=.identity], [thisArg])

This method is like _.forOwn except that it iterates over properties of object in the opposite order.

Arguments

  1. object (Object): The object to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.
  3. [thisArg] (*): The this binding of iteratee.

Returns

(Object): Returns object.

Example

  1. function Foo() {
  2. this.a = 1;
  3. this.b = 2;
  4. }
  5. Foo.prototype.c = 3;
  6. _.forOwnRight(new Foo, function(value, key) {
  7. console.log(key);
  8. });
  9. // => logs 'b' and 'a' assuming `_.forOwn` logs 'a' and 'b'

_.functions(object)

Creates an array of function property names from all enumerable properties, own and inherited, of object.

Aliases

_.methods

Arguments

  1. object (Object): The object to inspect.

Returns

(Array): Returns the new array of property names.

Example

  1. _.functions(_);
  2. // => ['after', 'ary', 'assign', ...]

_.get(object, path, [defaultValue])

Gets the property value at path of object. If the resolved value is undefined the defaultValue is used in its place.

Arguments

  1. object (Object): The object to query.
  2. path (Array|string): The path of the property to get.
  3. [defaultValue] (*): The value returned if the resolved value is undefined.

Returns

(*): Returns the resolved value.

Example

  1. var object = { 'a': [{ 'b': { 'c': 3 } }] };
  2. _.get(object, 'a[0].b.c');
  3. // => 3
  4. _.get(object, ['a', '0', 'b', 'c']);
  5. // => 3
  6. _.get(object, 'a.b.c', 'default');
  7. // => 'default'

_.has(object, path)

Checks if path is a direct property.

Arguments

  1. object (Object): The object to query.
  2. path (Array|string): The path to check.

Returns

(boolean): Returns true if path is a direct property, else false.

Example

  1. var object = { 'a': { 'b': { 'c': 3 } } };
  2. _.has(object, 'a');
  3. // => true
  4. _.has(object, 'a.b.c');
  5. // => true
  6. _.has(object, ['a', 'b', 'c']);
  7. // => true

_.invert(object, [multiValue])

Creates an object composed of the inverted keys and values of object. If object contains duplicate values, subsequent values overwrite property assignments of previous values unless multiValue is true.

Arguments

  1. object (Object): The object to invert.
  2. [multiValue] (boolean): Allow multiple values per key.

Returns

(Object): Returns the new inverted object.

Example

  1. var object = { 'a': 1, 'b': 2, 'c': 1 };
  2. _.invert(object);
  3. // => { '1': 'c', '2': 'b' }
  4. // with `multiValue`
  5. _.invert(object, true);
  6. // => { '1': ['a', 'c'], '2': ['b'] }

_.keys(object)

Creates an array of the own enumerable property names of object.

Note: Non-object values are coerced to objects. See the ES spec for more details.

Arguments

  1. object (Object): The object to query.

Returns

(Array): Returns the array of property names.

Example

  1. function Foo() {
  2. this.a = 1;
  3. this.b = 2;
  4. }
  5. Foo.prototype.c = 3;
  6. _.keys(new Foo);
  7. // => ['a', 'b'] (iteration order is not guaranteed)
  8. _.keys('hi');
  9. // => ['0', '1']

_.keysIn(object)

Creates an array of the own and inherited enumerable property names of object.

Note: Non-object values are coerced to objects.

Arguments

  1. object (Object): The object to query.

Returns

(Array): Returns the array of property names.

Example

  1. function Foo() {
  2. this.a = 1;
  3. this.b = 2;
  4. }
  5. Foo.prototype.c = 3;
  6. _.keysIn(new Foo);
  7. // => ['a', 'b', 'c'] (iteration order is not guaranteed)

.mapKeys(object, [iteratee=.identity], [thisArg])

` # [Ⓣ][1]

The opposite of _.mapValues; this method creates an object with the same values as object and keys generated by running each own enumerable property of object through iteratee.

Arguments

  1. object (Object): The object to iterate over.
  2. [iteratee=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of iteratee.

Returns

(Object): Returns the new mapped object.

Example

  1. _.mapKeys({ 'a': 1, 'b': 2 }, function(value, key) {
  2. return key + value;
  3. });
  4. // => { 'a1': 1, 'b2': 2 }

.mapValues(object, [iteratee=.identity], [thisArg])

Creates an object with the same keys as object and values generated by running each own enumerable property of object through iteratee. The iteratee function is bound to thisArg and invoked with three arguments:
(value, key, object).

If a property name is provided for iteratee the created _.property style callback returns the property value of the given element.

If a value is also provided for thisArg the created _.matchesProperty style callback returns true for elements that have a matching property value, else false.

If an object is provided for iteratee the created _.matches style callback returns true for elements that have the properties of the given object, else false.

Arguments

  1. object (Object): The object to iterate over.
  2. [iteratee=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of iteratee.

Returns

(Object): Returns the new mapped object.

Example

  1. _.mapValues({ 'a': 1, 'b': 2 }, function(n) {
  2. return n * 3;
  3. });
  4. // => { 'a': 3, 'b': 6 }
  5. var users = {
  6. 'fred': { 'user': 'fred', 'age': 40 },
  7. 'pebbles': { 'user': 'pebbles', 'age': 1 }
  8. };
  9. // using the `_.property` callback shorthand
  10. _.mapValues(users, 'age');
  11. // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)

_.merge(object, [sources], [customizer], [thisArg])

Recursively merges own enumerable properties of the source object(s), that don’t resolve to undefined into the destination object. Subsequent sources overwrite property assignments of previous sources. If customizer is provided it’s invoked to produce the merged values of the destination and source properties. If customizer returns undefined merging is handled by the method instead. The customizer is bound to thisArg and invoked with five arguments: (objectValue, sourceValue, key, object, source).

Arguments

  1. object (Object): The destination object.
  2. [sources] (…Object): The source objects.
  3. [customizer] (Function): The function to customize assigned values.
  4. [thisArg] (*): The this binding of customizer.

Returns

(Object): Returns object.

Example

  1. var users = {
  2. 'data': [{ 'user': 'barney' }, { 'user': 'fred' }]
  3. };
  4. var ages = {
  5. 'data': [{ 'age': 36 }, { 'age': 40 }]
  6. };
  7. _.merge(users, ages);
  8. // => { 'data': [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }] }
  9. // using a customizer callback
  10. var object = {
  11. 'fruits': ['apple'],
  12. 'vegetables': ['beet']
  13. };
  14. var other = {
  15. 'fruits': ['banana'],
  16. 'vegetables': ['carrot']
  17. };
  18. _.merge(object, other, function(a, b) {
  19. if (_.isArray(a)) {
  20. return a.concat(b);
  21. }
  22. });
  23. // => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot'] }

_.omit(object, [predicate], [thisArg])

The opposite of _.pick; this method creates an object composed of the own and inherited enumerable properties of object that are not omitted.

Arguments

  1. object (Object): The source object.
  2. [predicate] (Function|…(string|string[]): The function invoked per iteration or property names to omit, specified as individual property names or arrays of property names.
  3. [thisArg] (*): The this binding of predicate.

Returns

(Object): Returns the new object.

Example

  1. var object = { 'user': 'fred', 'age': 40 };
  2. _.omit(object, 'age');
  3. // => { 'user': 'fred' }
  4. _.omit(object, _.isNumber);
  5. // => { 'user': 'fred' }

_.pairs(object)

Creates a two dimensional array of the key-value pairs for object, e.g. [[key1, value1], [key2, value2]].

Arguments

  1. object (Object): The object to query.

Returns

(Array): Returns the new array of key-value pairs.

Example

  1. _.pairs({ 'barney': 36, 'fred': 40 });
  2. // => [['barney', 36], ['fred', 40]] (iteration order is not guaranteed)

_.pick(object, [predicate], [thisArg])

Creates an object composed of the picked object properties. Property names may be specified as individual arguments or as arrays of property names. If predicate is provided it’s invoked for each property of object picking the properties predicate returns truthy for. The predicate is bound to thisArg and invoked with three arguments: (value, key, object).

Arguments

  1. object (Object): The source object.
  2. [predicate] (Function|…(string|string[]): The function invoked per iteration or property names to pick, specified as individual property names or arrays of property names.
  3. [thisArg] (*): The this binding of predicate.

Returns

(Object): Returns the new object.

Example

  1. var object = { 'user': 'fred', 'age': 40 };
  2. _.pick(object, 'user');
  3. // => { 'user': 'fred' }
  4. _.pick(object, _.isString);
  5. // => { 'user': 'fred' }

_.result(object, path, [defaultValue])

This method is like _.get except that if the resolved value is a function it’s invoked with the this binding of its parent object and its result is returned.

Arguments

  1. object (Object): The object to query.
  2. path (Array|string): The path of the property to resolve.
  3. [defaultValue] (*): The value returned if the resolved value is undefined.

Returns

(*): Returns the resolved value.

Example

  1. var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };
  2. _.result(object, 'a[0].b.c1');
  3. // => 3
  4. _.result(object, 'a[0].b.c2');
  5. // => 4
  6. _.result(object, 'a.b.c', 'default');
  7. // => 'default'
  8. _.result(object, 'a.b.c', _.constant('default'));
  9. // => 'default'

_.set(object, path, value)

Sets the property value of path on object. If a portion of path does not exist it’s created.

Arguments

  1. object (Object): The object to augment.
  2. path (Array|string): The path of the property to set.
  3. value (*): The value to set.

Returns

(Object): Returns object.

Example

  1. var object = { 'a': [{ 'b': { 'c': 3 } }] };
  2. _.set(object, 'a[0].b.c', 4);
  3. console.log(object.a[0].b.c);
  4. // => 4
  5. _.set(object, 'x[0].y.z', 5);
  6. console.log(object.x[0].y.z);
  7. // => 5

.transform(object, [iteratee=.identity], [accumulator], [thisArg])

An alternative to _.reduce; this method transforms object to a new accumulator object which is the result of running each of its own enumerable properties through iteratee, with each invocation potentially mutating the accumulator object. The iteratee is bound to thisArg and invoked with four arguments: (accumulator, value, key, object). Iteratee functions may exit iteration early by explicitly returning false.

Arguments

  1. object (Array|Object): The object to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.
  3. [accumulator] (*): The custom accumulator value.
  4. [thisArg] (*): The this binding of iteratee.

Returns

(*): Returns the accumulated value.

Example

  1. _.transform([2, 3, 4], function(result, n) {
  2. result.push(n *= n);
  3. return n % 2 == 0;
  4. });
  5. // => [4, 9]
  6. _.transform({ 'a': 1, 'b': 2 }, function(result, n, key) {
  7. result[key] = n * 3;
  8. });
  9. // => { 'a': 3, 'b': 6 }

_.values(object)

Creates an array of the own enumerable property values of object.

Note: Non-object values are coerced to objects.

Arguments

  1. object (Object): The object to query.

Returns

(Array): Returns the array of property values.

Example

  1. function Foo() {
  2. this.a = 1;
  3. this.b = 2;
  4. }
  5. Foo.prototype.c = 3;
  6. _.values(new Foo);
  7. // => [1, 2] (iteration order is not guaranteed)
  8. _.values('hi');
  9. // => ['h', 'i']

_.valuesIn(object)

Creates an array of the own and inherited enumerable property values of object.

Note: Non-object values are coerced to objects.

Arguments

  1. object (Object): The object to query.

Returns

(Array): Returns the array of property values.

Example

  1. function Foo() {
  2. this.a = 1;
  3. this.b = 2;
  4. }
  5. Foo.prototype.c = 3;
  6. _.valuesIn(new Foo);
  7. // => [1, 2, 3] (iteration order is not guaranteed)