_(value)

Creates a lodash object which wraps value to enable implicit method chain sequences. 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 sequence and return the unwrapped value. Otherwise, the value must be unwrapped with _#value.

Explicit chain sequences, which must be unwrapped with _#value, may be enabled using _.chain.

The execution of chained methods is lazy, that is, it’s deferred until _#value is implicitly or explicitly called.

Lazy evaluation allows several methods to support shortcut fusion. Shortcut fusion is an optimization to merge iteratee calls; this avoids the creation of intermediate arrays and can greatly reduce the number of iteratee executions. Sections of a chain sequence qualify for shortcut fusion if the section is applied to an array and iteratees accept only one argument. The heuristic for whether a section qualifies for shortcut fusion is subject to change.

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, shift, sort, splice, and unshift

The wrapper String methods are:
replace and split

The wrapper methods that support shortcut fusion are:
at, compact, drop, dropRight, dropWhile, filter, find, findLast, head, initial, last, map, reject, reverse, slice, tail, take, takeRight, takeRightWhile, takeWhile, and toArray

The chainable wrapper methods are:
after, ary, assign, assignIn, assignInWith, assignWith, at, before, bind, bindAll, bindKey, castArray, chain, chunk, commit, compact, concat, conforms, constant, countBy, create, curry, debounce, defaults, defaultsDeep, defer, delay, difference, differenceBy, differenceWith, drop, dropRight, dropRightWhile, dropWhile, extend, extendWith, fill, filter, flatMap, flatMapDeep, flatMapDepth, flatten, flattenDeep, flattenDepth, flip, flow, flowRight, fromPairs, functions, functionsIn, groupBy, initial, intersection, intersectionBy, intersectionWith, invert, invertBy, invokeMap, iteratee, keyBy, keys, keysIn, map, mapKeys, mapValues, matches, matchesProperty, memoize, merge, mergeWith, method, methodOf, mixin, negate, nthArg, omit, omitBy, once, orderBy, over, overArgs, overEvery, overSome, partial, partialRight, partition, pick, pickBy, plant, property, propertyOf, pull, pullAll, pullAllBy, pullAllWith, pullAt, push, range, rangeRight, rearg, reject, remove, rest, reverse, sampleSize, set, setWith, shuffle, slice, sort, sortBy, splice, spread, tail, take, takeRight, takeRightWhile, takeWhile, tap, throttle, thru, toArray, toPairs, toPairsIn, toPath, toPlainObject, transform, unary, union, unionBy, unionWith, uniq, uniqBy, uniqWith, unset, unshift, unzip, unzipWith, update, updateWith, values, valuesIn, without, wrap, xor, xorBy, xorWith, zip, zipObject, zipObjectDeep, and zipWith

The wrapper methods that are not chainable by default are:
add, attempt, camelCase, capitalize, ceil, clamp, clone, cloneDeep, cloneDeepWith, cloneWith, conformsTo, deburr, defaultTo, divide, each, eachRight, endsWith, eq, escape, escapeRegExp, every, find, findIndex, findKey, findLast, findLastIndex, findLastKey, first, floor, forEach, forEachRight, forIn, forInRight, forOwn, forOwnRight, get, gt, gte, has, hasIn, head, identity, includes, indexOf, inRange, invoke, isArguments, isArray, isArrayBuffer, isArrayLike, isArrayLikeObject, isBoolean, isBuffer, isDate, isElement, isEmpty, isEqual, isEqualWith, isError, isFinite, isFunction, isInteger, isLength, isMap, isMatch, isMatchWith, isNaN, isNative, isNil, isNull, isNumber, isObject, isObjectLike, isPlainObject, isRegExp, isSafeInteger, isSet, isString, isUndefined, isTypedArray, isWeakMap, isWeakSet, join, kebabCase, last, lastIndexOf, lowerCase, lowerFirst, lt, lte, max, maxBy, mean, meanBy, min, minBy, multiply, noConflict, noop, now, nth, pad, padEnd, padStart, parseInt, pop, random, reduce, reduceRight, repeat, result, round, runInContext, sample, shift, size, snakeCase, some, sortedIndex, sortedIndexBy, sortedLastIndex, sortedLastIndexBy, startCase, startsWith, stubArray, stubFalse, stubObject, stubString, stubTrue, subtract, sum, sumBy, template, times, toFinite, toInteger, toJSON, toLength, toLower, toNumber, toSafeInteger, toString, toUpper, trim, trimEnd, trimStart, truncate, unescape, uniqueId, upperCase, upperFirst, value, and words

Arguments

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

Returns

(Object): Returns the new lodash wrapper instance.

Example

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

_.chain(value)

Creates a lodash wrapper instance that wraps value with explicit method chain sequences enabled. The result of such sequences must be unwrapped with _#value.

Since

1.3.0

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 = _
  7. .chain(users)
  8. .sortBy('age')
  9. .map(function(o) {
  10. return o.user + ' is ' + o.age;
  11. })
  12. .head()
  13. .value();
  14. // => 'pebbles is 1'

_.tap(value, interceptor)

This method invokes interceptor and returns value. The interceptor is invoked with one argument; (value). The purpose of this method is to “tap into” a method chain sequence in order to modify intermediate results.

Since

0.1.0

Arguments

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

Returns

(*): Returns value.

Example

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

_.thru(value, interceptor)

This method is like _.tap except that it returns the result of interceptor. The purpose of this method is to “pass thru” values replacing intermediate results in a method chain sequence.

Since

3.0.0

Arguments

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

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[Symbol.iterator]()

Enables the wrapper to be iterable.

Since

4.0.0

Returns

(Object): Returns the wrapper object.

Example

  1. var wrapped = _([1, 2]);
  2. wrapped[Symbol.iterator]() === wrapped;
  3. // => true
  4. Array.from(wrapped);
  5. // => [1, 2]

_.prototype.at([paths])

This method is the wrapper version of _.at.

Since

1.0.0

Arguments

  1. [paths] (…(string|string[])): The property paths to pick.

Returns

(Object): Returns the new lodash wrapper instance.

Example

  1. var object = { 'a': [{ 'b': { 'c': 3 } }, 4] };
  2. _(object).at(['a[0].b.c', 'a[1]']).value();
  3. // => [3, 4]

_.prototype.chain()

Creates a lodash wrapper instance with explicit method chain sequences enabled.

Since

0.1.0

Returns

(Object): Returns the new lodash wrapper instance.

Example

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

_.prototype.commit()

Executes the chain sequence and returns the wrapped result.

Since

3.2.0

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.next()

Gets the next value on a wrapped object following the iterator protocol.

Since

4.0.0

Returns

(Object): Returns the next iterator value.

Example

  1. var wrapped = _([1, 2]);
  2. wrapped.next();
  3. // => { 'done': false, 'value': 1 }
  4. wrapped.next();
  5. // => { 'done': false, 'value': 2 }
  6. wrapped.next();
  7. // => { 'done': true, 'value': undefined }

_.prototype.plant(value)

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

Since

3.2.0

Arguments

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

Returns

(Object): Returns the new lodash wrapper instance.

Example

  1. function square(n) {
  2. return n * n;
  3. }
  4. var wrapped = _([1, 2]).map(square);
  5. var other = wrapped.plant([3, 4]);
  6. other.value();
  7. // => [9, 16]
  8. wrapped.value();
  9. // => [1, 4]

_.prototype.reverse()

This method is the wrapper version of _.reverse.

Note: This method mutates the wrapped array.

Since

0.1.0

Returns

(Object): Returns the new 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.value()

Executes the chain sequence to resolve the unwrapped value.

Since

0.1.0

Aliases

.prototype.toJSON, .prototype.valueOf

Returns

(*): Returns the resolved unwrapped value.

Example

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