_(value)
创建一个包装 value
的 lodash
对象以启用隐式方法链序列。操作和返回数组、集合和函数的方法可以链接在一起。检索单个值或可能返回原始值的方法将自动结束链序列并返回未包装的值。否则,该值必须使用 _#value
展开。
必须使用 _#value
解包的显式链序列可以使用 _.chain
启用。
链式方法的执行是惰性的,也就是说,它会延迟到 _#value
被隐式或显式调用。
惰性评估允许多种方法支持快捷方式融合。 Shortcut fusion 是一种合并 iteratee 调用的优化;这避免了中间数组的创建,并且可以大大减少迭代执行的次数。如果将链序列的部分应用于数组并且迭代器只接受一个参数,则链序列的部分有资格进行快捷融合。一个部分是否有资格进行快捷融合的启发式可能会发生变化。
只要 _#value
方法直接或间接包含在构建中,自定义构建中就支持链接。
除了 lodash 方法,包装器还有 Array
和 String
方法。
包装器 Array
方法是:concat
、join
、pop
、push
、shift
、sort
、splice
和unshift
包装器String
方法是:replace
和 split
支持快捷方式融合的包装方法有:at
, compact
, drop
, dropRight
, dropWhile
, filter
, find
, findLast
, head
, initial
, last
, map
, reject
, reverse
, slice
, tail
, take
, takeRight
, takeRightWhile
, takeWhile
, toArray
可链接的包装器方法是: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
, zipWith
默认情况下不可链接的包装器方法是: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
, words
引入版本 0.1.0
参数
value
(*): 包装在lodash
实例中的值。
返回
(Object): 返回新的 lodash
包装器实例。
示例
function square(n) {
return n * n
}
const wrapped = _([1, 2, 3])
wrapped.reduce(_.add)
const squares = wrapped.map(square)
_.isArray(squares)
_.isArray(squares.value())
_.chain
创建一个 lodash
包装器实例,它包装 value
并启用显式方法链序列。此类序列的结果必须使用 _#value
展开。
引入版本 1.3.0
参数
value
(*): 要包装的值。
返回
(Object): 返回新的 lodash
包装器实例。
示例
const users = [{ user: 'barney', age: 36 }, { user: 'fred', age: 40 }, { user: 'pebbles', age: 1 }]
const youngest = _.chain(users).sortBy('age').map((o) => {
return `${o.user} is ${o.age}`
}).head().value()
_.tap
此方法调用 interceptor
并返回 value
。使用一个参数调用拦截器; (价值)。此方法的目的是“利用”方法链序列以修改中间结果。
引入版本 0.1.0
参数
value
(*): 提供给interceptor
的值。interceptor
(Function): 要调用的函数。
返回
(*): 返回“价值”。
示例
_([1, 2, 3]).tap((array) => {
array.pop()
}).reverse().value()
_.thru
此方法类似于 _.tap
,只是它返回 interceptor
的结果。此方法的目的是“通过”值替换方法链序列中的中间结果。
引入版本 3.0.0
参数
value
(*): 提供给interceptor
的值。interceptor
(Function): 要调用的函数。
返回
(*): 返回 interceptor
的结果。
示例
_(' abc ').chain().trim().thru((value) => {
return [value]
}).value()
_.prototypeSymbol.iterator
使包装器可迭代。
引入版本 4.0.0
返回
(Object): 返回包装对象。
示例
const wrapped = _([1, 2])
wrapped[Symbol.iterator]() === wrapped
Array.from(wrapped)
_.prototype.at
此方法是 _.at
的包装器版本。
引入版本 1.0.0
参数
[paths]
(...(string|string[])): 要选择的属性路径。
返回
(Object): 返回新的 lodash
包装器实例。
示例
const object = { a: [{ b: { c: 3 } }, 4] }
_(object).at(['a[0].b.c', 'a[1]']).value()
_.prototype.chain
创建一个启用显式方法链序列的 lodash
包装器实例。
引入版本 0.1.0
返回
(Object): 返回新的 lodash
包装器实例。
示例
const users = [{ user: 'barney', age: 36 }, { user: 'fred', age: 40 }]
_(users).head()
_(users).chain().head().pick('user').value()
_.prototype.commit
执行链序列并返回包装的结果。
引入版本 3.2.0
返回
(Object): 返回新的 lodash
包装器实例。
示例
const array = [1, 2]
let wrapped = _(array).push(3)
console.log(array)
wrapped = wrapped.commit()
console.log(array)
wrapped.last()
console.log(array)
_.prototype.next
获取遵循 iterator 协议 的包装对象的下一个值。
引入版本 4.0.0
返回
(Object): 返回下一个迭代器值。
示例
const wrapped = _([1, 2])
wrapped.next()
wrapped.next()
wrapped.next()
_.prototype.plant
创建链序列的克隆,将 value
种植为包装的值。
引入版本 3.2.0
参数
value
(*): 种植价值。
返回
(Object): 返回新的 lodash
包装器实例。
示例
function square(n) {
return n * n
}
const wrapped = _([1, 2]).map(square)
const other = wrapped.plant([3, 4])
other.value()
wrapped.value()
_.prototype.reverse
此方法是 _.reverse
的包装器版本。
注意: 此方法会改变包装的数组。
引入版本 0.1.0
返回
(Object): 返回新的 lodash
包装器实例。
示例
const array = [1, 2, 3]
_(array).reverse().value()
console.log(array)
_.prototype.value
执行链序列来解析解包的值。
引入版本 0.1.0
Aliases
_.prototype.toJSON, _.prototype.valueOf
返回
(*): 返回已解析的展开值。