_.attempt(func)

Attempts to invoke func, returning either the result or the caught error object. Any additional arguments are provided to func when it’s invoked.

Arguments

  1. func (Function): The function to attempt.

Returns

(*): Returns the func result or error object.

Example

  1. // avoid throwing errors for invalid selectors
  2. var elements = _.attempt(function(selector) {
  3. return document.querySelectorAll(selector);
  4. }, '>_>');
  5. if (_.isError(elements)) {
  6. elements = [];
  7. }

.callback([func=.identity], [thisArg])

Creates a function that invokes func with the this binding of thisArg and arguments of the created function. If func is a property name the created callback returns the property value for a given element. If func is an object the created callback returns true for elements that contain the equivalent object properties, otherwise it returns false.

Aliases

_.iteratee

Arguments

  1. [func=_.identity] (*): The value to convert to a callback.
  2. [thisArg] (*): The this binding of func.

Returns

(Function): Returns the callback.

Example

  1. var users = [
  2. { 'user': 'barney', 'age': 36 },
  3. { 'user': 'fred', 'age': 40 }
  4. ];
  5. // wrap to create custom callback shorthands
  6. _.callback = _.wrap(_.callback, function(callback, func, thisArg) {
  7. var match = /^(.+?)__([gl]t)(.+)$/.exec(func);
  8. if (!match) {
  9. return callback(func, thisArg);
  10. }
  11. return function(object) {
  12. return match[2] == 'gt'
  13. ? object[match[1]] > match[3]
  14. : object[match[1]] < match[3];
  15. };
  16. });
  17. _.filter(users, 'age__gt36');
  18. // => [{ 'user': 'fred', 'age': 40 }]

_.constant(value)

Creates a function that returns value.

Arguments

  1. value (*): The value to return from the new function.

Returns

(Function): Returns the new function.

Example

  1. var object = { 'user': 'fred' };
  2. var getter = _.constant(object);
  3. getter() === object;
  4. // => true

_.identity(value)

This method returns the first argument provided to it.

Arguments

  1. value (*): Any value.

Returns

(*): Returns value.

Example

  1. var object = { 'user': 'fred' };
  2. _.identity(object) === object;
  3. // => true

_.matches(source)

Creates a function that performs a deep comparison between a given object and source, returning true if the given object has equivalent property values, else false.

Note: This method supports comparing arrays, booleans, Date objects, numbers, Object objects, regexes, and strings. Objects are compared by their own, not inherited, enumerable properties. For comparing a single own or inherited property value see _.matchesProperty.

Arguments

  1. source (Object): The object of property values to match.

Returns

(Function): Returns the new function.

Example

  1. var users = [
  2. { 'user': 'barney', 'age': 36, 'active': true },
  3. { 'user': 'fred', 'age': 40, 'active': false }
  4. ];
  5. _.filter(users, _.matches({ 'age': 40, 'active': false }));
  6. // => [{ 'user': 'fred', 'age': 40, 'active': false }]

_.matchesProperty(path, srcValue)

Creates a function that compares the property value of path on a given object to value.

Note: This method supports comparing arrays, booleans, Date objects, numbers, Object objects, regexes, and strings. Objects are compared by their own, not inherited, enumerable properties.

Arguments

  1. path (Array|string): The path of the property to get.
  2. srcValue (*): The value to match.

Returns

(Function): Returns the new function.

Example

  1. var users = [
  2. { 'user': 'barney' },
  3. { 'user': 'fred' }
  4. ];
  5. _.find(users, _.matchesProperty('user', 'fred'));
  6. // => { 'user': 'fred' }

_.method(path, [args])

Creates a function that invokes the method at path on a given object. Any additional arguments are provided to the invoked method.

Arguments

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

Returns

(Function): Returns the new function.

Example

  1. var objects = [
  2. { 'a': { 'b': { 'c': _.constant(2) } } },
  3. { 'a': { 'b': { 'c': _.constant(1) } } }
  4. ];
  5. _.map(objects, _.method('a.b.c'));
  6. // => [2, 1]
  7. _.invoke(_.sortBy(objects, _.method(['a', 'b', 'c'])), 'a.b.c');
  8. // => [1, 2]

_.methodOf(object, [args])

The opposite of _.method; this method creates a function that invokes the method at a given path on object. Any additional arguments are provided to the invoked method.

Arguments

  1. object (Object): The object to query.
  2. [args] (…*): The arguments to invoke the method with.

Returns

(Function): Returns the new function.

Example

  1. var array = _.times(3, _.constant),
  2. object = { 'a': array, 'b': array, 'c': array };
  3. _.map(['a[2]', 'c[0]'], _.methodOf(object));
  4. // => [2, 0]
  5. _.map([['a', '2'], ['c', '0']], _.methodOf(object));
  6. // => [2, 0]

_.mixin([object=lodash], source, [options])

Adds all own enumerable function properties of a source object to the destination object. If object is a function then methods are added to its prototype as well.

Note: Use _.runInContext to create a pristine lodash function to avoid conflicts caused by modifying the original.

Arguments

  1. [object=lodash] (Function|Object): The destination object.
  2. source (Object): The object of functions to add.
  3. [options] (Object): The options object.
  4. [options.chain=true] (boolean): Specify whether the functions added are chainable.

Returns

(Function|Object): Returns object.

Example

  1. function vowels(string) {
  2. return _.filter(string, function(v) {
  3. return /[aeiou]/i.test(v);
  4. });
  5. }
  6. _.mixin({ 'vowels': vowels });
  7. _.vowels('fred');
  8. // => ['e']
  9. _('fred').vowels().value();
  10. // => ['e']
  11. _.mixin({ 'vowels': vowels }, { 'chain': false });
  12. _('fred').vowels();
  13. // => ['e']

_.noConflict()`

# [Ⓣ][1]

Reverts the _ variable to its previous value and returns a reference to the lodash function.

Returns

(Function): Returns the lodash function.

Example

  1. var lodash = _.noConflict();

_.noop()

A no-operation function that returns undefined regardless of the arguments it receives.

Example

  1. var object = { 'user': 'fred' };
  2. _.noop(object) === undefined;
  3. // => true

_.property(path)

Creates a function that returns the property value at path on a given object.

Arguments

  1. path (Array|string): The path of the property to get.

Returns

(Function): Returns the new function.

Example

  1. var objects = [
  2. { 'a': { 'b': { 'c': 2 } } },
  3. { 'a': { 'b': { 'c': 1 } } }
  4. ];
  5. _.map(objects, _.property('a.b.c'));
  6. // => [2, 1]
  7. _.pluck(_.sortBy(objects, _.property(['a', 'b', 'c'])), 'a.b.c');
  8. // => [1, 2]

_.propertyOf(object)

The opposite of _.property; this method creates a function that returns the property value at a given path on object.

Arguments

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

Returns

(Function): Returns the new function.

Example

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

_.range([start=0], end, [step=1])

Creates an array of numbers (positive and/or negative) progressing from start up to, but not including, end. If end is not specified it’s set to start with start then set to 0. If end is less than start a zero-length range is created unless a negative step is specified.

Arguments

  1. [start=0] (number): The start of the range.
  2. end (number): The end of the range.
  3. [step=1] (number): The value to increment or decrement by.

Returns

(Array): Returns the new array of numbers.

Example

  1. _.range(4);
  2. // => [0, 1, 2, 3]
  3. _.range(1, 5);
  4. // => [1, 2, 3, 4]
  5. _.range(0, 20, 5);
  6. // => [0, 5, 10, 15]
  7. _.range(0, -4, -1);
  8. // => [0, -1, -2, -3]
  9. _.range(1, 4, 0);
  10. // => [1, 1, 1]
  11. _.range(0);
  12. // => []

_.runInContext([context=root])

Create a new pristine lodash function using the given context object.

Arguments

  1. [context=root] (Object): The context object.

Returns

(Function): Returns a new lodash function.

Example

  1. _.mixin({ 'foo': _.constant('foo') });
  2. var lodash = _.runInContext();
  3. lodash.mixin({ 'bar': lodash.constant('bar') });
  4. _.isFunction(_.foo);
  5. // => true
  6. _.isFunction(_.bar);
  7. // => false
  8. lodash.isFunction(lodash.foo);
  9. // => false
  10. lodash.isFunction(lodash.bar);
  11. // => true
  12. // using `context` to mock `Date#getTime` use in `_.now`
  13. var mock = _.runInContext({
  14. 'Date': function() {
  15. return { 'getTime': getTimeMock };
  16. }
  17. });
  18. // or creating a suped-up `defer` in Node.js
  19. var defer = _.runInContext({ 'setTimeout': setImmediate }).defer;

.times(n, [iteratee=.identity], [thisArg])

Invokes the iteratee function n times, returning an array of the results of each invocation. The iteratee is bound to thisArg and invoked with one argument; (index).

Arguments

  1. n (number): The number of times to invoke iteratee.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.
  3. [thisArg] (*): The this binding of iteratee.

Returns

(Array): Returns the array of results.

Example

  1. var diceRolls = _.times(3, _.partial(_.random, 1, 6, false));
  2. // => [3, 6, 4]
  3. _.times(3, function(n) {
  4. mage.castSpell(n);
  5. });
  6. // => invokes `mage.castSpell(n)` three times with `n` of `0`, `1`, and `2`
  7. _.times(3, function(n) {
  8. this.cast(n);
  9. }, mage);
  10. // => also invokes `mage.castSpell(n)` three times

_.uniqueId([prefix])

Generates a unique ID. If prefix is provided the ID is appended to it.

Arguments

  1. [prefix] (string): The value to prefix the ID with.

Returns

(string): Returns the unique ID.

Example

  1. _.uniqueId('contact_');
  2. // => 'contact_104'
  3. _.uniqueId();
  4. // => '105'