_.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 = { 'name': 'fred' };
  2. var getter = _.constant(object);
  3. getter() === object;
  4. // => true

_.createCallback([func=identity], [thisArg], [argCount])

Produces a callback bound to an optional thisArg. If func is a property name the created callback will return the property value for a given element. If func is an object the created callback will return true for elements that contain the equivalent object properties, otherwise it will return false.

Arguments

  1. [func=identity] (*): The value to convert to a callback.
  2. [thisArg] (*): The this binding of the created callback.
  3. [argCount] (number): The number of arguments the callback accepts.

Returns

(Function): Returns a callback function.

Example

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

_.escape(string)

Converts the characters &, <, >, ", and ' in string to their corresponding HTML entities.

Arguments

  1. string (string): The string to escape.

Returns

(string): Returns the escaped string.

Example

  1. _.escape('Fred, Wilma, & Pebbles');
  2. // => 'Fred, Wilma, &amp; Pebbles'

_.identity(value)

This method returns the first argument provided to it.

Arguments

  1. value (*): Any value.

Returns

(*): Returns value.

Example

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

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

Adds function properties of a source object to the destination object. If object is a function methods will be added to its prototype as well.

Arguments

  1. [object=lodash] (Function|Object): 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.

Example

  1. function capitalize(string) {
  2. return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
  3. }
  4. _.mixin({ 'capitalize': capitalize });
  5. _.capitalize('fred');
  6. // => 'Fred'
  7. _('fred').capitalize().value();
  8. // => 'Fred'
  9. _.mixin({ 'capitalize': capitalize }, { 'chain': false });
  10. _('fred').capitalize();
  11. // => 'Fred'

_.noConflict()

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.

Example

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

_.now

Gets the number of milliseconds that have elapsed since the Unix epoch (1 January 1970 00:00:00 UTC).

Example

  1. var stamp = _.now();
  2. _.defer(function() { console.log(_.now() - stamp); });
  3. // => logs the number of milliseconds it took for the deferred function to be called

_.parseInt(value, [radix])

Converts the given value into an integer of the specified radix. If radix is undefined or 0 a radix of 10 is used unless the value is a hexadecimal, in which case a radix of 16 is used.

Note: This method avoids differences in native ES3 and ES5 parseInt implementations. See http://es5.github.io/#E.

Arguments

  1. value (string): The value to parse.
  2. [radix] (number): The radix used to interpret the value to parse.

Returns

(number): Returns the new integer value.

Example

  1. _.parseInt('08');
  2. // => 8

_.property(key)

Creates a “_.pluck” style function, which returns the key value of a given object.

Arguments

  1. key (string): The name of the property to retrieve.

Returns

(Function): Returns the new function.

Example

  1. var characters = [
  2. { 'name': 'fred', 'age': 40 },
  3. { 'name': 'barney', 'age': 36 }
  4. ];
  5. var getName = _.property('name');
  6. _.map(characters, getName);
  7. // => ['barney', 'fred']
  8. _.sortBy(characters, getName);
  9. // => [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }]

_.random([min=0], [max=1], [floating=false])

Produces a random number between min and max (inclusive). If only one argument is provided a number between 0 and the given number will be returned. If floating is truey or either min or max are floats a floating-point number will be returned instead of an integer.

Arguments

  1. [min=0] (number): The minimum possible value.
  2. [max=1] (number): The maximum possible value.
  3. [floating=false] (boolean): Specify returning a floating-point number.

Returns

(number): Returns a random number.

Example

  1. _.random(0, 5);
  2. // => an integer between 0 and 5
  3. _.random(5);
  4. // => also an integer between 0 and 5
  5. _.random(5, true);
  6. // => a floating-point number between 0 and 5
  7. _.random(1.2, 5.2);
  8. // => a floating-point number between 1.2 and 5.2

_.result(object, key)

Resolves the value of property key on object. If key is a function it will be invoked with the this binding of object and its result returned, else the property value is returned. If object is falsey then undefined is returned.

Arguments

  1. object (Object): The object to inspect.
  2. key (string): The name of the property to resolve.

Returns

(*): Returns the resolved value.

Example

  1. var object = {
  2. 'cheese': 'crumpets',
  3. 'stuff': function() {
  4. return 'nonsense';
  5. }
  6. };
  7. _.result(object, 'cheese');
  8. // => 'crumpets'
  9. _.result(object, 'stuff');
  10. // => 'nonsense'

_.runInContext([context=root])

Create a new lodash function using the given context object.

Arguments

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

Returns

(Function): Returns the lodash function.


_.template(text, data, [options], [sourceURL], [variable])

A micro-templating method that handles arbitrary delimiters, preserves whitespace, and correctly escapes quotes within interpolated code.

Note: In the development build, _.template utilizes sourceURLs for easier debugging. See http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl

For more information on precompiling templates see:
https://lodash.com/custom-builds

For more information on Chrome extension sandboxes see:
http://developer.chrome.com/stable/extensions/sandboxingEval.html

Arguments

  1. text (string): The template text.
  2. data (Object): The data object used to populate the text.
  3. [options] (Object): The options object.
  4. [options.escape] (RegExp): The “escape” delimiter.
  5. [options.evaluate] (RegExp): The “evaluate” delimiter.
  6. [options.imports] (Object): An object to import into the template as local variables.
  7. [options.interpolate] (RegExp): The “interpolate” delimiter.
  8. [sourceURL] (string): The sourceURL of the template’s compiled source.
  9. [variable] (string): The data object variable name.

Returns

(Function|string): Returns a compiled function when no data object is given, else it returns the interpolated text.

Example

  1. // using the "interpolate" delimiter to create a compiled template
  2. var compiled = _.template('hello <%= name %>');
  3. compiled({ 'name': 'fred' });
  4. // => 'hello fred'
  5. // using the "escape" delimiter to escape HTML in data property values
  6. _.template('<b><%- value %></b>', { 'value': '<script>' });
  7. // => '<b>&lt;script&gt;</b>'
  8. // using the "evaluate" delimiter to generate HTML
  9. var list = '<% _.forEach(people, function(name) { %><li><%- name %></li><% }); %>';
  10. _.template(list, { 'people': ['fred', 'barney'] });
  11. // => '<li>fred</li><li>barney</li>'
  12. // using the ES6 delimiter as an alternative to the default "interpolate" delimiter
  13. _.template('hello ${ name }', { 'name': 'pebbles' });
  14. // => 'hello pebbles'
  15. // using the internal `print` function in "evaluate" delimiters
  16. _.template('<% print("hello " + name); %>!', { 'name': 'barney' });
  17. // => 'hello barney!'
  18. // using a custom template delimiters
  19. _.templateSettings = {
  20. 'interpolate': /{{([\s\S]+?)}}/g
  21. };
  22. _.template('hello {{ name }}!', { 'name': 'mustache' });
  23. // => 'hello mustache!'
  24. // using the `imports` option to import jQuery
  25. var list = '<% jq.each(people, function(name) { %><li><%- name %></li><% }); %>';
  26. _.template(list, { 'people': ['fred', 'barney'] }, { 'imports': { 'jq': jQuery } });
  27. // => '<li>fred</li><li>barney</li>'
  28. // using the `sourceURL` option to specify a custom sourceURL for the template
  29. var compiled = _.template('hello <%= name %>', null, { 'sourceURL': '/basic/greeting.jst' });
  30. compiled(data);
  31. // => find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector
  32. // using the `variable` option to ensure a with-statement isn't used in the compiled template
  33. var compiled = _.template('hi <%= data.name %>!', null, { 'variable': 'data' });
  34. compiled.source;
  35. // => function(data) {
  36. var __t, __p = '', __e = _.escape;
  37. __p += 'hi ' + ((__t = ( data.name )) == null ? '' : __t) + '!';
  38. return __p;
  39. }
  40. // using the `source` property to inline compiled templates for meaningful
  41. // line numbers in error messages and a stack trace
  42. fs.writeFileSync(path.join(cwd, 'jst.js'), '\
  43. var JST = {\
  44. "main": ' + _.template(mainText).source + '\
  45. };\
  46. ');

_.times(n, callback, [thisArg])

Executes the callback n times, returning an array of the results of each callback execution. The callback is bound to thisArg and invoked with one argument; (index).

Arguments

  1. n (number): The number of times to execute the callback.
  2. callback (Function): The function called per iteration.
  3. [thisArg] (*): The this binding of callback.

Returns

(Array): Returns an array of the results of each callback execution.

Example

  1. var diceRolls = _.times(3, _.partial(_.random, 1, 6));
  2. // => [3, 6, 4]
  3. _.times(3, function(n) { mage.castSpell(n); });
  4. // => calls `mage.castSpell(n)` three times, passing `n` of `0`, `1`, and `2` respectively
  5. _.times(3, function(n) { this.cast(n); }, mage);
  6. // => also calls `mage.castSpell(n)` three times

_.unescape(string)

The inverse of _.escape this method converts the HTML entities &amp;, &lt;, &gt;, &quot;, and &#39; in string to their corresponding characters.

Arguments

  1. string (string): The string to unescape.

Returns

(string): Returns the unescaped string.

Example

  1. _.unescape('Fred, Barney &amp; Pebbles');
  2. // => 'Fred, Barney & Pebbles'

_.uniqueId([prefix])

Generates a unique ID. If prefix is provided the ID will be 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'