_.assign(object [, source1, source2, …, callback, thisArg])

Assigns own enumerable properties of source object(s) to the destination object. Subsequent sources will overwrite property assignments of previous sources. If a callback function is passed, it will be executed to produce the assigned values. The callback is bound to thisArg and invoked with two arguments; (objectValue, sourceValue).

Aliases

extend

Arguments

  1. object (Object): The destination object.
  2. [source1, source2, ...] (Object): The source objects.
  3. [callback] (Function): The function to customize assigning values.
  4. [thisArg] (Mixed): The this binding of callback.

Returns

(Object): Returns the destination object.

Example

  1. _.assign({ 'name': 'moe' }, { 'age': 40 });
  2. // => { 'name': 'moe', 'age': 40 }
  3. var defaults = _.partialRight(_.assign, function(a, b) {
  4. return typeof a == 'undefined' ? b : a;
  5. });
  6. var food = { 'name': 'apple' };
  7. defaults(food, { 'name': 'banana', 'type': 'fruit' });
  8. // => { 'name': 'apple', 'type': 'fruit' }

_.clone(value [, deep=false, callback, thisArg])

Creates a clone of value. If deep is true, nested objects will also be cloned, otherwise they will be assigned by reference. If a callback function is passed, it will be executed to produce the cloned values. If callback returns undefined, cloning will be handled by the method instead. The callback is bound to thisArg and invoked with one argument; (value).

Arguments

  1. value (Mixed): The value to clone.
  2. [deep=false] (Boolean): A flag to indicate a deep clone.
  3. [callback] (Function): The function to customize cloning values.
  4. [thisArg] (Mixed): The this binding of callback.

Returns

(Mixed): Returns the cloned value.

Example

  1. var stooges = [
  2. { 'name': 'moe', 'age': 40 },
  3. { 'name': 'larry', 'age': 50 }
  4. ];
  5. var shallow = _.clone(stooges);
  6. shallow[0] === stooges[0];
  7. // => true
  8. var deep = _.clone(stooges, true);
  9. deep[0] === stooges[0];
  10. // => false
  11. _.mixin({
  12. 'clone': _.partialRight(_.clone, function(value) {
  13. return _.isElement(value) ? value.cloneNode(false) : undefined;
  14. })
  15. });
  16. var clone = _.clone(document.body);
  17. clone.childNodes.length;
  18. // => 0

_.cloneDeep(value [, callback, thisArg])

Creates a deep clone of value. If a callback function is passed, it will be executed to produce the cloned values. If callback returns undefined, cloning will be handled by the method instead. The callback is bound to thisArg and invoked with one argument; (value).

Note: This method is loosely based on the structured clone algorithm. Functions and DOM nodes are not cloned. The enumerable properties of arguments objects and objects created by constructors other than Object are cloned to plain Object objects. See http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm.

Arguments

  1. value (Mixed): The value to deep clone.
  2. [callback] (Function): The function to customize cloning values.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Mixed): Returns the deep cloned value.

Example

  1. var stooges = [
  2. { 'name': 'moe', 'age': 40 },
  3. { 'name': 'larry', 'age': 50 }
  4. ];
  5. var deep = _.cloneDeep(stooges);
  6. deep[0] === stooges[0];
  7. // => false
  8. var view = {
  9. 'label': 'docs',
  10. 'node': element
  11. };
  12. var clone = _.cloneDeep(view, function(value) {
  13. return _.isElement(value) ? value.cloneNode(true) : undefined;
  14. });
  15. clone.node == view.node;
  16. // => false

_.defaults(object [, source1, source2, …])

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 defaults of the same property will be ignored.

Arguments

  1. object (Object): The destination object.
  2. [source1, source2, ...] (Object): The source objects.

Returns

(Object): Returns the destination object.

Example

  1. var food = { 'name': 'apple' };
  2. _.defaults(food, { 'name': 'banana', 'type': 'fruit' });
  3. // => { 'name': 'apple', 'type': 'fruit' }

_.findKey(object [, callback=identity, thisArg])

This method is similar to _.find, except that it returns the key of the element that passes the callback check, instead of the element itself.

Arguments

  1. object (Object): The object to search.
  2. [callback=identity] (Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a “.pluck” or “.where” style callback, respectively.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Mixed): Returns the key of the found element, else undefined.

Example

  1. _.findKey({ 'a': 1, 'b': 2, 'c': 3, 'd': 4 }, function(num) {
  2. return num % 2 == 0;
  3. });
  4. // => 'b'

_.forIn(object [, callback=identity, thisArg])

Iterates over object‘s own and inherited enumerable properties, executing the callback for each property. The callback is bound to thisArg and invoked with three arguments; (value, key, object). Callbacks may exit iteration early by explicitly returning false.

Arguments

  1. object (Object): The object to iterate over.
  2. [callback=identity] (Function): The function called per iteration.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Object): Returns object.

Example

  1. function Dog(name) {
  2. this.name = name;
  3. }
  4. Dog.prototype.bark = function() {
  5. alert('Woof, woof!');
  6. };
  7. _.forIn(new Dog('Dagny'), function(value, key) {
  8. alert(key);
  9. });
  10. // => alerts 'name' and 'bark' (order is not guaranteed)

_.forOwn(object [, callback=identity, thisArg])

Iterates over an object’s own enumerable properties, executing the callback for each property. The callback is bound to thisArg and invoked with three arguments; (value, key, object). Callbacks may exit iteration early by explicitly returning false.

Arguments

  1. object (Object): The object to iterate over.
  2. [callback=identity] (Function): The function called per iteration.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Object): Returns object.

Example

  1. _.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
  2. alert(key);
  3. });
  4. // => alerts '0', '1', and 'length' (order is not guaranteed)

_.functions(object)

Creates a sorted array of all enumerable properties, own and inherited, of object that have function values.

Aliases

methods

Arguments

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

Returns

(Array): Returns a new array of property names that have function values.

Example

  1. _.functions(_);
  2. // => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]

_.has(object, property)

Checks if the specified object property exists and is a direct property, instead of an inherited property.

Arguments

  1. object (Object): The object to check.
  2. property (String): The property to check for.

Returns

(Boolean): Returns true if key is a direct property, else false.

Example

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

_.invert(object)

Creates an object composed of the inverted keys and values of the given object.

Arguments

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

Returns

(Object): Returns the created inverted object.

Example

  1. _.invert({ 'first': 'moe', 'second': 'larry' });
  2. // => { 'moe': 'first', 'larry': 'second' }

_.isArguments(value)

Checks if value is an arguments object.

Arguments

  1. value (Mixed): The value to check.

Returns

(Boolean): Returns true, if the value is an arguments object, else false.

Example

  1. (function() { return _.isArguments(arguments); })(1, 2, 3);
  2. // => true
  3. _.isArguments([1, 2, 3]);
  4. // => false

_.isArray(value)

Checks if value is an array.

Arguments

  1. value (Mixed): The value to check.

Returns

(Boolean): Returns true, if the value is an array, else false.

Example

  1. (function() { return _.isArray(arguments); })();
  2. // => false
  3. _.isArray([1, 2, 3]);
  4. // => true

_.isBoolean(value)

Checks if value is a boolean value.

Arguments

  1. value (Mixed): The value to check.

Returns

(Boolean): Returns true, if the value is a boolean value, else false.

Example

  1. _.isBoolean(null);
  2. // => false

_.isDate(value)

Checks if value is a date.

Arguments

  1. value (Mixed): The value to check.

Returns

(Boolean): Returns true, if the value is a date, else false.

Example

  1. _.isDate(new Date);
  2. // => true

_.isElement(value)

Checks if value is a DOM element.

Arguments

  1. value (Mixed): The value to check.

Returns

(Boolean): Returns true, if the value is a DOM element, else false.

Example

  1. _.isElement(document.body);
  2. // => true

_.isEmpty(value)

Checks if value is empty. Arrays, strings, or arguments objects with a length of 0 and objects with no own enumerable properties are considered “empty”.

Arguments

  1. value (Array|Object|String): The value to inspect.

Returns

(Boolean): Returns true, if the value is empty, else false.

Example

  1. _.isEmpty([1, 2, 3]);
  2. // => false
  3. _.isEmpty({});
  4. // => true
  5. _.isEmpty('');
  6. // => true

_.isEqual(a, b [, callback, thisArg])

Performs a deep comparison between two values to determine if they are equivalent to each other. If callback is passed, it will be executed to compare values. If callback returns undefined, comparisons will be handled by the method instead. The callback is bound to thisArg and invoked with two arguments; (a, b).

Arguments

  1. a (Mixed): The value to compare.
  2. b (Mixed): The other value to compare.
  3. [callback] (Function): The function to customize comparing values.
  4. [thisArg] (Mixed): The this binding of callback.

Returns

(Boolean): Returns true, if the values are equivalent, else false.

Example

  1. var moe = { 'name': 'moe', 'age': 40 };
  2. var copy = { 'name': 'moe', 'age': 40 };
  3. moe == copy;
  4. // => false
  5. _.isEqual(moe, copy);
  6. // => true
  7. var words = ['hello', 'goodbye'];
  8. var otherWords = ['hi', 'goodbye'];
  9. _.isEqual(words, otherWords, function(a, b) {
  10. var reGreet = /^(?:hello|hi)$/i,
  11. aGreet = _.isString(a) && reGreet.test(a),
  12. bGreet = _.isString(b) && reGreet.test(b);
  13. return (aGreet || bGreet) ? (aGreet == bGreet) : undefined;
  14. });
  15. // => true

_.isFinite(value)

Checks if value is, or can be coerced to, a finite number.

Note: This is not the same as native isFinite, which will return true for booleans and empty strings. See http://es5.github.com/#x15.1.2.5.

Arguments

  1. value (Mixed): The value to check.

Returns

(Boolean): Returns true, if the value is finite, else false.

Example

  1. _.isFinite(-101);
  2. // => true
  3. _.isFinite('10');
  4. // => true
  5. _.isFinite(true);
  6. // => false
  7. _.isFinite('');
  8. // => false
  9. _.isFinite(Infinity);
  10. // => false

_.isFunction(value)

Checks if value is a function.

Arguments

  1. value (Mixed): The value to check.

Returns

(Boolean): Returns true, if the value is a function, else false.

Example

  1. _.isFunction(_);
  2. // => true

_.isNaN(value)

Checks if value is NaN.

Note: This is not the same as native isNaN, which will return true for undefined and other values. See http://es5.github.com/#x15.1.2.4.

Arguments

  1. value (Mixed): The value to check.

Returns

(Boolean): Returns true, if the value is NaN, else false.

Example

  1. _.isNaN(NaN);
  2. // => true
  3. _.isNaN(new Number(NaN));
  4. // => true
  5. isNaN(undefined);
  6. // => true
  7. _.isNaN(undefined);
  8. // => false

_.isNull(value)

Checks if value is null.

Arguments

  1. value (Mixed): The value to check.

Returns

(Boolean): Returns true, if the value is null, else false.

Example

  1. _.isNull(null);
  2. // => true
  3. _.isNull(undefined);
  4. // => false

_.isNumber(value)

Checks if value is a number.

Arguments

  1. value (Mixed): The value to check.

Returns

(Boolean): Returns true, if the value is a number, else false.

Example

  1. _.isNumber(8.4 * 5);
  2. // => true

_.isObject(value)

Checks if value is the language type of Object. (e.g. arrays, functions, objects, regexes, new Number(0), and new String(''))

Arguments

  1. value (Mixed): The value to check.

Returns

(Boolean): Returns true, if the value is an object, else false.

Example

  1. _.isObject({});
  2. // => true
  3. _.isObject([1, 2, 3]);
  4. // => true
  5. _.isObject(1);
  6. // => false

_.isPlainObject(value)

Checks if a given value is an object created by the Object constructor.

Arguments

  1. value (Mixed): The value to check.

Returns

(Boolean): Returns true, if value is a plain object, else false.

Example

  1. function Stooge(name, age) {
  2. this.name = name;
  3. this.age = age;
  4. }
  5. _.isPlainObject(new Stooge('moe', 40));
  6. // => false
  7. _.isPlainObject([1, 2, 3]);
  8. // => false
  9. _.isPlainObject({ 'name': 'moe', 'age': 40 });
  10. // => true

_.isRegExp(value)

Checks if value is a regular expression.

Arguments

  1. value (Mixed): The value to check.

Returns

(Boolean): Returns true, if the value is a regular expression, else false.

Example

  1. _.isRegExp(/moe/);
  2. // => true

_.isString(value)

Checks if value is a string.

Arguments

  1. value (Mixed): The value to check.

Returns

(Boolean): Returns true, if the value is a string, else false.

Example

  1. _.isString('moe');
  2. // => true

_.isUndefined(value)

Checks if value is undefined.

Arguments

  1. value (Mixed): The value to check.

Returns

(Boolean): Returns true, if the value is undefined, else false.

Example

  1. _.isUndefined(void 0);
  2. // => true

_.keys(object)

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

Arguments

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

Returns

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

Example

  1. _.keys({ 'one': 1, 'two': 2, 'three': 3 });
  2. // => ['one', 'two', 'three'] (order is not guaranteed)

_.merge(object [, source1, source2, …, callback, thisArg])

Recursively merges own enumerable properties of the source object(s), that don’t resolve to undefined, into the destination object. Subsequent sources will overwrite property assignments of previous sources. If a callback function is passed, it will be executed to produce the merged values of the destination and source properties. If callback returns undefined, merging will be handled by the method instead. The callback is bound to thisArg and invoked with two arguments; (objectValue, sourceValue).

Arguments

  1. object (Object): The destination object.
  2. [source1, source2, ...] (Object): The source objects.
  3. [callback] (Function): The function to customize merging properties.
  4. [thisArg] (Mixed): The this binding of callback.

Returns

(Object): Returns the destination object.

Example

  1. var names = {
  2. 'stooges': [
  3. { 'name': 'moe' },
  4. { 'name': 'larry' }
  5. ]
  6. };
  7. var ages = {
  8. 'stooges': [
  9. { 'age': 40 },
  10. { 'age': 50 }
  11. ]
  12. };
  13. _.merge(names, ages);
  14. // => { 'stooges': [{ 'name': 'moe', 'age': 40 }, { 'name': 'larry', 'age': 50 }] }
  15. var food = {
  16. 'fruits': ['apple'],
  17. 'vegetables': ['beet']
  18. };
  19. var otherFood = {
  20. 'fruits': ['banana'],
  21. 'vegetables': ['carrot']
  22. };
  23. _.merge(food, otherFood, function(a, b) {
  24. return _.isArray(a) ? a.concat(b) : undefined;
  25. });
  26. // => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot] }

_.omit(object, callback|[prop1, prop2, …, thisArg])

Creates a shallow clone of object excluding the specified properties. Property names may be specified as individual arguments or as arrays of property names. If a callback function is passed, it will be executed for each property in the object, omitting the properties callback returns truthy for. The callback is bound to thisArg and invoked with three arguments; (value, key, object).

Arguments

  1. object (Object): The source object.
  2. callback|[prop1, prop2, ...] (Function|String): The properties to omit or the function called per iteration.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Object): Returns an object without the omitted properties.

Example

  1. _.omit({ 'name': 'moe', 'age': 40 }, 'age');
  2. // => { 'name': 'moe' }
  3. _.omit({ 'name': 'moe', 'age': 40 }, function(value) {
  4. return typeof value == 'number';
  5. });
  6. // => { 'name': 'moe' }

_.pairs(object)

Creates a two dimensional array of the given object’s key-value pairs, i.e. [[key1, value1], [key2, value2]].

Arguments

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

Returns

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

Example

  1. _.pairs({ 'moe': 30, 'larry': 40 });
  2. // => [['moe', 30], ['larry', 40]] (order is not guaranteed)

_.pick(object, callback|[prop1, prop2, …, thisArg])

Creates a shallow clone of object composed of the specified properties. Property names may be specified as individual arguments or as arrays of property names. If callback is passed, it will be executed for each property in the object, picking the properties callback returns truthy for. The callback is bound to thisArg and invoked with three arguments; (value, key, object).

Arguments

  1. object (Object): The source object.
  2. callback|[prop1, prop2, ...] (Array|Function|String): The function called per iteration or properties to pick, either as individual arguments or arrays.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Object): Returns an object composed of the picked properties.

Example

  1. _.pick({ 'name': 'moe', '_userid': 'moe1' }, 'name');
  2. // => { 'name': 'moe' }
  3. _.pick({ 'name': 'moe', '_userid': 'moe1' }, function(value, key) {
  4. return key.charAt(0) != '_';
  5. });
  6. // => { 'name': 'moe' }

_.transform(collection [, callback=identity, accumulator, thisArg])

An alternative to _.reduce, this method transforms an object to a new accumulator object which is the result of running each of its elements through the callback, with each callback execution potentially mutating the accumulator object. The callback is bound to thisArg and invoked with four arguments; (accumulator, value, key, object). Callbacks may exit iteration early by explicitly returning false.

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [callback=identity] (Function): The function called per iteration.
  3. [accumulator] (Mixed): The custom accumulator value.
  4. [thisArg] (Mixed): The this binding of callback.

Returns

(Mixed): Returns the accumulated value.

Example

  1. var squares = _.transform([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(result, num) {
  2. num *= num;
  3. if (num % 2) {
  4. return result.push(num) < 3;
  5. }
  6. });
  7. // => [1, 9, 25]
  8. var mapped = _.transform({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {
  9. result[key] = num * 3;
  10. });
  11. // => { 'a': 3, 'b': 6, 'c': 9 }

_.values(object)

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

Arguments

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

Returns

(Array): Returns a new array of property values.

Example

  1. _.values({ 'one': 1, 'two': 2, 'three': 3 });
  2. // => [1, 2, 3] (order is not guaranteed)