_(value)

Creates a lodash object which wraps the given value to enable intuitive method chaining.

In addition to Lo-Dash methods, wrappers also have the following Array methods:
concat, join, pop, push, reverse, shift, slice, sort, splice, and unshift

Chaining is supported in custom builds as long as the value method is implicitly or explicitly included in the build.

The chainable wrapper functions are:
after, assign, bind, bindAll, bindKey, chain, compact, compose, concat, countBy, create, createCallback, curry, debounce, defaults, defer, delay, difference, filter, flatten, forEach, forEachRight, forIn, forInRight, forOwn, forOwnRight, functions, groupBy, indexBy, initial, intersection, invert, invoke, keys, map, max, memoize, merge, min, object, omit, once, pairs, partial, partialRight, pick, pluck, pull, push, range, reject, remove, rest, reverse, shuffle, slice, sort, sortBy, splice, tap, throttle, times, toArray, transform, union, uniq, unshift, unzip, values, where, without, wrap, and zip

The non-chainable wrapper functions are:
clone, cloneDeep, contains, escape, every, find, findIndex, findKey, findLast, findLastIndex, findLastKey, has, identity, indexOf, isArguments, isArray, isBoolean, isDate, isElement, isEmpty, isEqual, isFinite, isFunction, isNaN, isNull, isNumber, isObject, isPlainObject, isRegExp, isString, isUndefined, join, lastIndexOf, mixin, noConflict, parseInt, pop, random, reduce, reduceRight, result, shift, size, some, sortedIndex, runInContext, template, unescape, uniqueId, and value

The wrapper functions first and last return wrapped values when n is provided, otherwise they return unwrapped values.

Explicit chaining can be enabled by using the _.chain method.

Arguments

  1. value (*): The value to wrap in a lodash instance.

Returns

(Object): Returns a lodash instance.

Example

  1. var wrapped = _([1, 2, 3]);
  2. // returns an unwrapped value
  3. wrapped.reduce(function(sum, num) {
  4. return sum + num;
  5. });
  6. // => 6
  7. // returns a wrapped value
  8. var squares = wrapped.map(function(num) {
  9. return num * num;
  10. });
  11. _.isArray(squares);
  12. // => false
  13. _.isArray(squares.value());
  14. // => true

_.chain(value)

Creates a lodash object that wraps the given value with explicit method chaining enabled.

Arguments

  1. value (*): The value to wrap.

Returns

(Object): Returns the wrapper object.

Example

  1. var characters = [
  2. { 'name': 'barney', 'age': 36 },
  3. { 'name': 'fred', 'age': 40 },
  4. { 'name': 'pebbles', 'age': 1 }
  5. ];
  6. var youngest = _.chain(characters)
  7. .sortBy('age')
  8. .map(function(chr) { return chr.name + ' is ' + chr.age; })
  9. .first()
  10. .value();
  11. // => 'pebbles is 1'

_.tap(value, interceptor)

Invokes interceptor with the value as the first argument and then returns value. The purpose of this method is to “tap into” a method chain in order to perform operations on intermediate results within the chain.

Arguments

  1. value (*): The value to provide to interceptor.
  2. interceptor (Function): The function to invoke.

Returns

(*): Returns value.

Example

  1. _([1, 2, 3, 4])
  2. .tap(function(array) { array.pop(); })
  3. .reverse()
  4. .value();
  5. // => [3, 2, 1]

_.prototype.chain()

Enables explicit method chaining on the wrapper object.

Returns

(*): Returns the wrapper object.

Example

  1. var characters = [
  2. { 'name': 'barney', 'age': 36 },
  3. { 'name': 'fred', 'age': 40 }
  4. ];
  5. // without explicit chaining
  6. _(characters).first();
  7. // => { 'name': 'barney', 'age': 36 }
  8. // with explicit chaining
  9. _(characters).chain()
  10. .first()
  11. .pick('age')
  12. .value();
  13. // => { 'age': 36 }

_.prototype.toString()

Produces the toString result of the wrapped value.

Returns

(string): Returns the string result.

Example

  1. _([1, 2, 3]).toString();
  2. // => '1,2,3'

_.prototype.valueOf()

Extracts the wrapped value.

Aliases

_.prototype.value

Returns

(*): Returns the wrapped value.

Example

  1. _([1, 2, 3]).valueOf();
  2. // => [1, 2, 3]