_.assign(object, [sources])

Assigns own enumerable string keyed properties of source objects to the destination object. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.

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

Since

0.10.0

Arguments

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

Returns

(Object): Returns object.

Example

  1. function Foo() {
  2. this.a = 1;
  3. }
  4. function Bar() {
  5. this.c = 3;
  6. }
  7. Foo.prototype.b = 2;
  8. Bar.prototype.d = 4;
  9. _.assign({ 'a': 0 }, new Foo, new Bar);
  10. // => { 'a': 1, 'c': 3 }

_.assignIn(object, [sources])

This method is like _.assign except that it iterates over own and inherited source properties.

Note: This method mutates object.

Since

4.0.0

Aliases

_.extend

Arguments

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

Returns

(Object): Returns object.

Example

  1. function Foo() {
  2. this.a = 1;
  3. }
  4. function Bar() {
  5. this.c = 3;
  6. }
  7. Foo.prototype.b = 2;
  8. Bar.prototype.d = 4;
  9. _.assignIn({ 'a': 0 }, new Foo, new Bar);
  10. // => { 'a': 1, 'b': 2, 'c': 3, 'd': 4 }

_.assignInWith(object, sources, [customizer])

This method is like _.assignIn except that it accepts customizer which is invoked to produce the assigned values. If customizer returns undefined, assignment is handled by the method instead. The customizer is invoked with five arguments: (objValue, srcValue, key, object, source).

Note: This method mutates object.

Since

4.0.0

Aliases

_.extendWith

Arguments

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

Returns

(Object): Returns object.

Example

  1. function customizer(objValue, srcValue) {
  2. return _.isUndefined(objValue) ? srcValue : objValue;
  3. }
  4. var defaults = _.partialRight(_.assignInWith, customizer);
  5. defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 });
  6. // => { 'a': 1, 'b': 2 }

_.assignWith(object, sources, [customizer])

This method is like _.assign except that it accepts customizer which is invoked to produce the assigned values. If customizer returns undefined, assignment is handled by the method instead. The customizer is invoked with five arguments: (objValue, srcValue, key, object, source).

Note: This method mutates object.

Since

4.0.0

Arguments

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

Returns

(Object): Returns object.

Example

  1. function customizer(objValue, srcValue) {
  2. return _.isUndefined(objValue) ? srcValue : objValue;
  3. }
  4. var defaults = _.partialRight(_.assignWith, customizer);
  5. defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 });
  6. // => { 'a': 1, 'b': 2 }

_.at(object, [paths])

Creates an array of values corresponding to paths of object.

Since

1.0.0

Arguments

  1. object (Object): The object to iterate over.
  2. [paths] (…(string|string[])): The property paths to pick.

Returns

(Array): Returns the picked values.

Example

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

_.create(prototype, [properties])

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

Since

2.3.0

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 and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to undefined. Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.

Note: This method mutates object.

Since

0.1.0

Arguments

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

Returns

(Object): Returns object.

Example

  1. _.defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 });
  2. // => { 'a': 1, 'b': 2 }

_.defaultsDeep(object, [sources])

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

Note: This method mutates object.

Since

3.10.0

Arguments

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

Returns

(Object): Returns object.

Example

  1. _.defaultsDeep({ 'a': { 'b': 2 } }, { 'a': { 'b': 1, 'c': 3 } });
  2. // => { 'a': { 'b': 2, 'c': 3 } }

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

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

Since

1.1.0

Arguments

  1. object (Object): The object to inspect.
  2. [predicate=_.identity] (Function): The function invoked per iteration.

Returns

(*): 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(o) { return o.age < 40; });
  7. // => 'barney' (iteration order is not guaranteed)
  8. // The `_.matches` iteratee shorthand.
  9. _.findKey(users, { 'age': 1, 'active': true });
  10. // => 'pebbles'
  11. // The `_.matchesProperty` iteratee shorthand.
  12. _.findKey(users, ['active', false]);
  13. // => 'fred'
  14. // The `_.property` iteratee shorthand.
  15. _.findKey(users, 'active');
  16. // => 'barney'

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

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

Since

2.0.0

Arguments

  1. object (Object): The object to inspect.
  2. [predicate=_.identity] (Function): The function invoked per iteration.

Returns

(*): 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(o) { return o.age < 40; });
  7. // => returns 'pebbles' assuming `_.findKey` returns 'barney'
  8. // The `_.matches` iteratee shorthand.
  9. _.findLastKey(users, { 'age': 36, 'active': true });
  10. // => 'barney'
  11. // The `_.matchesProperty` iteratee shorthand.
  12. _.findLastKey(users, ['active', false]);
  13. // => 'fred'
  14. // The `_.property` iteratee shorthand.
  15. _.findLastKey(users, 'active');
  16. // => 'pebbles'

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

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

Since

0.3.0

Arguments

  1. object (Object): The object to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.

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', then 'c' (iteration order is not guaranteed).

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

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

Since

2.0.0

Arguments

  1. object (Object): The object to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.

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', then 'a' assuming `_.forIn` logs 'a', 'b', then 'c'.

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

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

Since

0.3.0

Arguments

  1. object (Object): The object to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.

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' then 'b' (iteration order is not guaranteed).

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

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

Since

2.0.0

Arguments

  1. object (Object): The object to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.

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' then 'a' assuming `_.forOwn` logs 'a' then 'b'.

_.functions(object)

Creates an array of function property names from own enumerable properties of object.

Since

0.1.0

Arguments

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

Returns

(Array): Returns the function names.

Example

  1. function Foo() {
  2. this.a = _.constant('a');
  3. this.b = _.constant('b');
  4. }
  5. Foo.prototype.c = _.constant('c');
  6. _.functions(new Foo);
  7. // => ['a', 'b']

_.functionsIn(object)

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

Since

4.0.0

Arguments

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

Returns

(Array): Returns the function names.

Example

  1. function Foo() {
  2. this.a = _.constant('a');
  3. this.b = _.constant('b');
  4. }
  5. Foo.prototype.c = _.constant('c');
  6. _.functionsIn(new Foo);
  7. // => ['a', 'b', 'c']

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

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

Since

3.7.0

Arguments

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

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 of object.

Since

0.1.0

Arguments

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

Returns

(boolean): Returns true if path exists, else false.

Example

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

_.hasIn(object, path)

Checks if path is a direct or inherited property of object.

Since

4.0.0

Arguments

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

Returns

(boolean): Returns true if path exists, else false.

Example

  1. var object = _.create({ 'a': _.create({ 'b': 2 }) });
  2. _.hasIn(object, 'a');
  3. // => true
  4. _.hasIn(object, 'a.b');
  5. // => true
  6. _.hasIn(object, ['a', 'b']);
  7. // => true
  8. _.hasIn(object, 'b');
  9. // => false

_.invert(object)

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.

Since

0.7.0

Arguments

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

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' }

_.invertBy(object, [iteratee=_.identity])

This method is like _.invert except that the inverted object is generated from the results of running each element of object thru iteratee. The corresponding inverted value of each inverted key is an array of keys responsible for generating the inverted value. The iteratee is invoked with one argument: (value).

Since

4.1.0

Arguments

  1. object (Object): The object to invert.
  2. [iteratee=_.identity] (Function): The iteratee invoked per element.

Returns

(Object): Returns the new inverted object.

Example

  1. var object = { 'a': 1, 'b': 2, 'c': 1 };
  2. _.invertBy(object);
  3. // => { '1': ['a', 'c'], '2': ['b'] }
  4. _.invertBy(object, function(value) {
  5. return 'group' + value;
  6. });
  7. // => { 'group1': ['a', 'c'], 'group2': ['b'] }

_.invoke(object, path, [args])

Invokes the method at path of object.

Since

4.0.0

Arguments

  1. object (Object): The object to query.
  2. path (Array|string): The path of the method to invoke.
  3. [args] (…*): The arguments to invoke the method with.

Returns

(*): Returns the result of the invoked method.

Example

  1. var object = { 'a': [{ 'b': { 'c': [1, 2, 3, 4] } }] };
  2. _.invoke(object, 'a[0].b.c.slice', 1, 3);
  3. // => [2, 3]

_.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.

Since

0.1.0

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.

Since

3.0.0

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])

The opposite of _.mapValues; this method creates an object with the same values as object and keys generated by running each own enumerable string keyed property of object thru iteratee. The iteratee is invoked with three arguments: (value, key, object).

Since

3.8.0

Arguments

  1. object (Object): The object to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.

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])

Creates an object with the same keys as object and values generated by running each own enumerable string keyed property of object thru iteratee. The iteratee is invoked with three arguments:
(value, key, object).

Since

2.4.0

Arguments

  1. object (Object): The object to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.

Returns

(Object): Returns the new mapped object.

Example

  1. var users = {
  2. 'fred': { 'user': 'fred', 'age': 40 },
  3. 'pebbles': { 'user': 'pebbles', 'age': 1 }
  4. };
  5. _.mapValues(users, function(o) { return o.age; });
  6. // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
  7. // The `_.property` iteratee shorthand.
  8. _.mapValues(users, 'age');
  9. // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)

_.merge(object, [sources])

This method is like _.assign except that it recursively merges own and inherited enumerable string keyed properties of source objects into the destination object. Source properties that resolve to undefined are skipped if a destination value exists. Array and plain object properties are merged recursively. Other objects and value types are overridden by assignment. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.

Note: This method mutates object.

Since

0.5.0

Arguments

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

Returns

(Object): Returns object.

Example

  1. var object = {
  2. 'a': [{ 'b': 2 }, { 'd': 4 }]
  3. };
  4. var other = {
  5. 'a': [{ 'c': 3 }, { 'e': 5 }]
  6. };
  7. _.merge(object, other);
  8. // => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }

_.mergeWith(object, sources, customizer)

This method is like _.merge except that it accepts customizer which is 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 invoked with six arguments:
(objValue, srcValue, key, object, source, stack).

Note: This method mutates object.

Since

4.0.0

Arguments

  1. object (Object): The destination object.
  2. sources (…Object): The source objects.
  3. customizer (Function): The function to customize assigned values.

Returns

(Object): Returns object.

Example

  1. function customizer(objValue, srcValue) {
  2. if (_.isArray(objValue)) {
  3. return objValue.concat(srcValue);
  4. }
  5. }
  6. var object = { 'a': [1], 'b': [2] };
  7. var other = { 'a': [3], 'b': [4] };
  8. _.mergeWith(object, other, customizer);
  9. // => { 'a': [1, 3], 'b': [2, 4] }

_.omit(object, [paths])

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

Note: This method is considerably slower than _.pick.

Since

0.1.0

Arguments

  1. object (Object): The source object.
  2. [paths] (…(string|string[])): The property paths to omit.

Returns

(Object): Returns the new object.

Example

  1. var object = { 'a': 1, 'b': '2', 'c': 3 };
  2. _.omit(object, ['a', 'c']);
  3. // => { 'b': '2' }

_.omitBy(object, [predicate=_.identity])

The opposite of _.pickBy; this method creates an object composed of the own and inherited enumerable string keyed properties of object that predicate doesn’t return truthy for. The predicate is invoked with two arguments: (value, key).

Since

4.0.0

Arguments

  1. object (Object): The source object.
  2. [predicate=_.identity] (Function): The function invoked per property.

Returns

(Object): Returns the new object.

Example

  1. var object = { 'a': 1, 'b': '2', 'c': 3 };
  2. _.omitBy(object, _.isNumber);
  3. // => { 'b': '2' }

_.pick(object, [paths])

Creates an object composed of the picked object properties.

Since

0.1.0

Arguments

  1. object (Object): The source object.
  2. [paths] (…(string|string[])): The property paths to pick.

Returns

(Object): Returns the new object.

Example

  1. var object = { 'a': 1, 'b': '2', 'c': 3 };
  2. _.pick(object, ['a', 'c']);
  3. // => { 'a': 1, 'c': 3 }

_.pickBy(object, [predicate=_.identity])

Creates an object composed of the object properties predicate returns truthy for. The predicate is invoked with two arguments: (value, key).

Since

4.0.0

Arguments

  1. object (Object): The source object.
  2. [predicate=_.identity] (Function): The function invoked per property.

Returns

(Object): Returns the new object.

Example

  1. var object = { 'a': 1, 'b': '2', 'c': 3 };
  2. _.pickBy(object, _.isNumber);
  3. // => { 'a': 1, 'c': 3 }

_.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.

Since

0.1.0

Arguments

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

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[0].b.c3', 'default');
  7. // => 'default'
  8. _.result(object, 'a[0].b.c3', _.constant('default'));
  9. // => 'default'

_.set(object, path, value)

Sets the value at path of object. If a portion of path doesn’t exist, it’s created. Arrays are created for missing index properties while objects are created for all other missing properties. Use _.setWith to customize path creation.

Note: This method mutates object.

Since

3.7.0

Arguments

  1. object (Object): The object to modify.
  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

_.setWith(object, path, value, [customizer])

This method is like _.set except that it accepts customizer which is invoked to produce the objects of path. If customizer returns undefined path creation is handled by the method instead. The customizer is invoked with three arguments: (nsValue, key, nsObject).

Note: This method mutates object.

Since

4.0.0

Arguments

  1. object (Object): The object to modify.
  2. path (Array|string): The path of the property to set.
  3. value (*): The value to set.
  4. [customizer] (Function): The function to customize assigned values.

Returns

(Object): Returns object.

Example

  1. var object = {};
  2. _.setWith(object, '[0][1]', 'a', Object);
  3. // => { '0': { '1': 'a' } }

_.toPairs(object)

Creates an array of own enumerable string keyed-value pairs for object which can be consumed by _.fromPairs. If object is a map or set, its entries are returned.

Since

4.0.0

Aliases

_.entries

Arguments

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

Returns

(Array): Returns the key-value pairs.

Example

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

_.toPairsIn(object)

Creates an array of own and inherited enumerable string keyed-value pairs for object which can be consumed by _.fromPairs. If object is a map or set, its entries are returned.

Since

4.0.0

Aliases

_.entriesIn

Arguments

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

Returns

(Array): Returns the key-value pairs.

Example

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

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

An alternative to _.reduce; this method transforms object to a new accumulator object which is the result of running each of its own enumerable string keyed properties thru iteratee, with each invocation potentially mutating the accumulator object. If accumulator is not provided, a new object with the same [[Prototype]] will be used. The iteratee is invoked with four arguments: (accumulator, value, key, object). Iteratee functions may exit iteration early by explicitly returning false.

Since

1.3.0

Arguments

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

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, 'c': 1 }, function(result, value, key) {
  7. (result[value] || (result[value] = [])).push(key);
  8. }, {});
  9. // => { '1': ['a', 'c'], '2': ['b'] }

_.unset(object, path)

Removes the property at path of object.

Note: This method mutates object.

Since

4.0.0

Arguments

  1. object (Object): The object to modify.
  2. path (Array|string): The path of the property to unset.

Returns

(boolean): Returns true if the property is deleted, else false.

Example

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

_.update(object, path, updater)

This method is like _.set except that accepts updater to produce the value to set. Use _.updateWith to customize path creation. The updater is invoked with one argument: (value).

Note: This method mutates object.

Since

4.6.0

Arguments

  1. object (Object): The object to modify.
  2. path (Array|string): The path of the property to set.
  3. updater (Function): The function to produce the updated value.

Returns

(Object): Returns object.

Example

  1. var object = { 'a': [{ 'b': { 'c': 3 } }] };
  2. _.update(object, 'a[0].b.c', function(n) { return n * n; });
  3. console.log(object.a[0].b.c);
  4. // => 9
  5. _.update(object, 'x[0].y.z', function(n) { return n ? n + 1 : 0; });
  6. console.log(object.x[0].y.z);
  7. // => 0

_.updateWith(object, path, updater, [customizer])

This method is like _.update except that it accepts customizer which is invoked to produce the objects of path. If customizer returns undefined path creation is handled by the method instead. The customizer is invoked with three arguments: (nsValue, key, nsObject).

Note: This method mutates object.

Since

4.6.0

Arguments

  1. object (Object): The object to modify.
  2. path (Array|string): The path of the property to set.
  3. updater (Function): The function to produce the updated value.
  4. [customizer] (Function): The function to customize assigned values.

Returns

(Object): Returns object.

Example

  1. var object = {};
  2. _.updateWith(object, '[0][1]', _.constant('a'), Object);
  3. // => { '0': { '1': 'a' } }

_.values(object)

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

Note: Non-object values are coerced to objects.

Since

0.1.0

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 string keyed property values of object.

Note: Non-object values are coerced to objects.

Since

3.0.0

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)