Lodash 中文文档 (v3.10.1) - “Chain” 方法

本文详细解析了Lodash库中的Chain方法,包括其用途、工作原理、支持的操作以及如何在实际场景中应用。通过示例演示了如何创建lodash对象并开启方法链,展示了链式调用的优势及限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Lodash 中文文档 (v3.10.1) - “Chain” 方法

Translated by PeckZeg
Original Docs: Lodash v3.10.1 Docs

“Chain” 方法

_(value)

创建一个包含 valuelodash 对象以开启内置的方法链。方法链对返回数组、集合或函数的方法产生作用,并且方法可以被链式调用。那些获取单值或可能返回一个原始值的方法将自动结束方法链并且返回一个未包裹成 lodash 对象的值。如果明确需要链式调用可以使用 _.chain。链式调用的加载将是延迟加载,这表明调用将延迟到间接或直接调用 _#value 方法。

延迟计算支持一些方法快速合并。快速合并是一个最优合并迭代器调用的策略。这样做可以帮助避免一些计算中间生成的数据结构,并且能够大大降低迭代器的执行次数。

链式调用支持自定义生成结果。只要在生成的时候直接或间接的包含 _#value 方法。

此外,对于 lodash 的方法,包装集拥有 ArrayString 的方法。

Array 包装集的方法有:

concat, join, pop, push, reverse, shift, slice, sort, spliceunshift

String 包装集的方法有:

replacesplit

支持快速合并的包装集方法有:

compact, drop, dropRight, dropRightWhile, dropWhile, filter, first, initial, last, map, pluck, reject, rest, reverse, slice, take, takeRight, takeRightWhile, takeWhile, toArraywhere

可链式调用的包装集方法有:

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

默认不能被链式调用的包装集方法有:

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, valuewords

包装集函数 sample 在参数提供 n 的情况下将返回包装集,其他情况下将返回未经包装过的值。

参数

  1. value (*) : 待包装至 lodash 实例的值

返回

(Object) : 返回一个新的 lodash 包装集实例

示例

var wrapped = _([1, 2, 3]);

// 返回一个未包装的值
wrapped.reduce(function(total, n) {
  return total + n;
});
// → 6

// 返回一个包装值
var squares = wrapped.map(function(n) {
  return n * n;
});

_.isArray(squares);
// → false

_.isArray(squares.value());
// → true

_.chain(value)

创建一个明确能够被链式调用的 lodash 对象

参数

  1. value (*) : 待包装的值

返回

(Object) : 返回一个新的 lodash 包装实例

示例

var users = [
  { 'user': 'barney',  'age': 36 },
  { 'user': 'fred',    'age': 40 },
  { 'user': 'pebbles', 'age': 1 }
];

var youngest = _.chain(users)
  .sortBy('age')
  .map(function(chr) {
    return chr.user + ' is ' + chr.age;
  })
  .first()
  .value();
// → 'pebbles is 1'

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

该方法执行 interceptor 并返回 value。该拦截器将绑定 thisArg 并在执行时传入一个参数:value。该方法的目的是“利用”方法链去操作链中的中间结果。

参数

  1. value (*) : 提供给拦截器的值

  2. interceptor (Function) : 待执行的函数

  3. [thisArg] (*) : interceptor 绑定的 this

返回

(*) : 返回值

示例

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

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

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

该方法类似 _.tap,但其返回拦截器的值。

参数

  1. value (*) : 提供给拦截器的值

  2. interceptor (Function) : 待执行的函数

  3. [thisArg] (*) : interceptor 绑定的 this

返回

(*) : 返回 interceptor 的结果

示例

_('  abc  ')
 .chain()
 .trim()
 .thru(function(value) {
   return [value];
 })
 .value();
// → ['abc']

_.prototype.chain()

在包装对象上显式开启链式调用

返回

(Object) : 返回一个新的 lodash 包装实例

示例

var users = [
  { 'user': 'barney', 'age': 36 },
  { 'user': 'fred',   'age': 40 }
];

// 不使用显式调用链
_(users).first();
// → { 'user': 'barney', 'age': 36 }

// 使用显式调用链
_(users).chain()
  .first()
  .pick('user')
  .value();
// → { 'user': 'barney' }

_.prototype.commit()

执行调用链队列并返回包装集结果

返回

(Object) : 返回新的 lodash 包装集实例

示例

var array = [1, 2];
var wrapped = _(array).push(3);

console.log(array);
// → [1, 2]

wrapped = wrapped.commit();
console.log(array);
// → [1, 2, 3]

wrapped.last();
// → 3

console.log(array);
// → [1, 2, 3]

_.prototype.concat([values])

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

创建一个含有连接其他数组和(或)值的新数组包装集。

参数

  1. [values] (…*) : 带连接的值

返回

(Array) : 返回一个已连接的新数组

示例

var array = [1];
var wrapped = _(array).concat(2, [3], [[4]]);

console.log(wrapped.value());
// → [1, 2, 3, [4]]

console.log(array);
// → [1]

_.prototype.plant()

创建一个链式调用队列的克隆(会将 value 替换为克隆后的链式调用链中)。

返回

(Object) : 返回一个新的 lodash 包装实例

示例

var array = [1, 2];
var wrapped = _(array).map(function(value) {
  return Math.pow(value, 2);
});

var other = [3, 4];
var otherWrapped = wrapped.plant(other);

otherWrapped.value();
// → [9, 16]

wrapped.value();
// → [1, 4]

_.prototype.reverse()

翻转包装数组,将第一个元素和最后一个元素对换,第二个元素和倒数第二个元素对换,以此类推。

注意:该方法会修改包装数组。

返回

(Object) : 返回一个已经翻转过的数组的 lodash 包装实例

示例

var array = [1, 2, 3];

_(array).reverse().value()
// → [3, 2, 1]

console.log(array);
// → [3, 2, 1]

_.prototype.toString()

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

产生强制将值转为未包装的字符串果。

返回

(string) : 返回强制转为字符串的值

示例

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

_.prototype.value()

执行方法链队列并提取未包装的值

别名

  • _.prototype.run

  • _.prototype.toJSON

  • _.prototype.valueOf

返回

(*) : 返回已处理的未包装的值

示例

_([1, 2, 3]).value();
// → [1, 2, 3]
<think>好的,用户遇到了下载matplotlib指定版本whl文件时的报错问题,具体是matplotlib 3.10.1 cp312 aarch64的版本。我需要先分析可能的原因。 首先,用户提供的版本组合是否合法?Matplotlib 3.10.1对应的CPython版本可能还不支持CP312,因为Python 3.12发布后,许多包需要时间适配。查阅官方PyPI页面,发现matplotlib 3.10.1确实不支持cp312,最高到cp311[^1]。因此,用户可能下载了不存在的文件,导致404错误。 其次,aarch64架构的支持情况。许多包的预编译whl可能没有为aarch64提供所有版本,尤其是较新的Python版本。需要确认是否存在该架构的whl文件。 解决方案方面,建议用户首先检查版本兼容性,降低Python版本到3.11,或者安装更高版本的matplotlib。如果必须使用Python 3.12,可能需要从源码编译,但依赖项较多,过程复杂。另外,使用镜像源或直接pip安装可能自动处理依赖,避免手动下载whl。 最后,生成相关问题时要覆盖常见安装问题、版本查询和替代方案,帮助用户扩展解决类似情况的能力。</think>### 解决matplotlib 3.10.1 cp312 aarch64 whl下载报错的分步指南 #### 1. 验证版本合法性 - **核心问题**:Python 3.12 (cp312) matplotlib 3.10.1 存在版本不兼容。根据PyPI官方记录,matplotlib 3.10.1最高仅支持到cp311标签[^1] - **验证方法**:访问 [PyPI matplotlib文件列表](https://pypi.org/project/matplotlib/3.10.1/#files),查看可用版本组合 #### 2. 解决方案选择 $$ \text{可行方案} = \begin{cases} ① \text{降低Python版本至3.11} \\ ② \text{升级matplotlib到支持3.12的版本} \\ ③ \text{源码编译安装} \end{cases} $$ #### 3. 具体实施步骤 **方案①:使用Python 3.11环境** ```bash # 创建虚拟环境 conda create -n py311 python=3.11 conda activate py311 pip install matplotlib==3.10.1 ``` **方案②:安装新版matplotlib** ```bash # 查看最新支持3.12的版本 pip install matplotlib --upgrade # 截至2024年1月,matplotlib≥3.8.0已支持Python3.12 ``` **方案③:源码编译安装(仅限必要情况)** ```bash sudo apt-get install python3-dev pkg-config libfreetype6-dev pip install git+https://github.com/matplotlib/matplotlib.git@v3.10.1 ``` #### 4. 架构兼容性说明 - aarch64架构建议优先使用conda安装: ```bash conda install -c conda-forge matplotlib=3.10.1 ``` - 若必须使用whl,可尝试通用版: ``` https://files.pythonhosted.org/packages/.../matplotlib-3.10.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值