_(value)

Creates a lodash object which wraps value to enable implicit chaining. Methods that operate on and return arrays, collections, and functions can be chained together. Methods that retrieve a single value or may return a primitive value will automatically end the chain returning the unwrapped value. Explicit chaining may be enabled using _.chain. The execution of chained methods is lazy, that is, execution is deferred until _#value is implicitly or explicitly called.

Lazy evaluation allows several methods to support shortcut fusion. Shortcut fusion is an optimization strategy which merge iteratee calls; this can help to avoid the creation of intermediate data structures and greatly reduce the number of iteratee executions.

Chaining is supported in custom builds as long as the _#value method is directly or indirectly included in the build.

In addition to lodash methods, wrappers have Array and String methods.

The wrapper Array methods are:
concat, join, pop, push, reverse, shift, slice, sort, splice, and unshift

The wrapper String methods are:
replace and split

The wrapper methods that support shortcut fusion are:
compact, drop, dropRight, dropRightWhile, dropWhile, filter, first, initial, last, map, pluck, reject, rest, reverse, slice, take, takeRight, takeRightWhile, takeWhile, toArray, and where

The chainable wrapper methods are:
after, ary, assign, at, before, bind, bindAll, bindKey, callback, chain, chunk, commit, compact, concat, constant, countBy, create, curry, debounce, defaults, defaultsDeep, defer, delay, difference, drop, dropRight, dropRightWhile, dropWhile, fill, filter, flatten, flattenDeep, flow, flowRight, forEach, forEachRight, forIn, forInRight, forOwn, forOwnRight, functions, groupBy, indexBy, initial, intersection, invert, invoke, keys, keysIn, map, mapKeys, mapValues, matches, matchesProperty, memoize, merge, method, methodOf, mixin, modArgs, negate, omit, once, pairs, partial, partialRight, partition, pick, plant, pluck, property, propertyOf, pull, pullAt, push, range, rearg, reject, remove, rest, restParam, reverse, set, shuffle, slice, sort, sortBy, sortByAll, sortByOrder, splice, spread, take, takeRight, takeRightWhile, takeWhile, tap, throttle, thru, times, toArray, toPlainObject, transform, union, uniq, unshift, unzip, unzipWith, values, valuesIn, where, without, wrap, xor, zip, zipObject, zipWith

The wrapper methods that are not chainable by default are:
add, attempt, camelCase, capitalize, ceil, clone, cloneDeep, deburr, endsWith, escape, escapeRegExp, every, find, findIndex, findKey, findLast, findLastIndex, findLastKey, findWhere, first, floor, get, gt, gte, has, identity, includes, indexOf, inRange, isArguments, isArray, isBoolean, isDate, isElement, isEmpty, isEqual, isError, isFinite isFunction, isMatch, isNative, isNaN, isNull, isNumber, isObject, isPlainObject, isRegExp, isString, isUndefined, isTypedArray, join, kebabCase, last, lastIndexOf, lt, lte, max, min, noConflict, noop, now, pad, padLeft, padRight, parseInt, pop, random, reduce, reduceRight, repeat, result, round, runInContext, shift, size, snakeCase, some, sortedIndex, sortedLastIndex, startCase, startsWith, sum, template, trim, trimLeft, trimRight, trunc, unescape, uniqueId, value, and words

The wrapper method sample will return a wrapped value when n is provided, otherwise an unwrapped value is returned.

Arguments

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

Returns

(Object): Returns the new lodash wrapper instance.

Example

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

_.chain(value)

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

Arguments

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

Returns

(Object): Returns the new lodash wrapper instance.

Example

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

_.tap(value, interceptor, [thisArg])

This method invokes interceptor and returns value. The interceptor is bound to thisArg and invoked with one argument; (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.
  3. [thisArg] (*): The this binding of interceptor.

Returns

(*): Returns value.

Example

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

_.thru(value, interceptor, [thisArg])

This method is like _.tap except that it returns the result of interceptor.

Arguments

  1. value (*): The value to provide to interceptor.
  2. interceptor (Function): The function to invoke.
  3. [thisArg] (*): The this binding of interceptor.

Returns

(*): Returns the result of interceptor.

Example

  1. _(' abc ')
  2. .chain()
  3. .trim()
  4. .thru(function(value) {
  5. return [value];
  6. })
  7. .value();
  8. // => ['abc']

_.prototype.chain()

Enables explicit method chaining on the wrapper object.

Returns

(Object): Returns the new lodash wrapper instance.

Example

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

_.prototype.commit()

Executes the chained sequence and returns the wrapped result.

Returns

(Object): Returns the new lodash wrapper instance.

Example

  1. var array = [1, 2];
  2. var wrapped = _(array).push(3);
  3. console.log(array);
  4. // => [1, 2]
  5. wrapped = wrapped.commit();
  6. console.log(array);
  7. // => [1, 2, 3]
  8. wrapped.last();
  9. // => 3
  10. console.log(array);
  11. // => [1, 2, 3]

_.prototype.concat([values])

Creates a new array joining a wrapped array with any additional arrays and/or values.

Arguments

  1. [values] (…*): The values to concatenate.

Returns

(Array): Returns the new concatenated array.

Example

  1. var array = [1];
  2. var wrapped = _(array).concat(2, [3], [[4]]);
  3. console.log(wrapped.value());
  4. // => [1, 2, 3, [4]]
  5. console.log(array);
  6. // => [1]

_.prototype.plant()

Creates a clone of the chained sequence planting value as the wrapped value.

Returns

(Object): Returns the new lodash wrapper instance.

Example

  1. var array = [1, 2];
  2. var wrapped = _(array).map(function(value) {
  3. return Math.pow(value, 2);
  4. });
  5. var other = [3, 4];
  6. var otherWrapped = wrapped.plant(other);
  7. otherWrapped.value();
  8. // => [9, 16]
  9. wrapped.value();
  10. // => [1, 4]

_.prototype.reverse()

Reverses the wrapped array so the first element becomes the last, the second element becomes the second to last, and so on.

Note: This method mutates the wrapped array.

Returns

(Object): Returns the new reversed lodash wrapper instance.

Example

  1. var array = [1, 2, 3];
  2. _(array).reverse().value()
  3. // => [3, 2, 1]
  4. console.log(array);
  5. // => [3, 2, 1]

_.prototype.toString()

Produces the result of coercing the unwrapped value to a string.

Returns

(string): Returns the coerced string value.

Example

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

_.prototype.value()

Executes the chained sequence to extract the unwrapped value.

Aliases

.prototype.run, .prototype.toJSON, _.prototype.valueOf

Returns

(*): Returns the resolved unwrapped value.

Example

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