资讯专栏INFORMATION COLUMN

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

张利勇 / 3390人阅读

摘要:别名参数待搜索的集合每次迭代执行的函数绑定的返回返回匹配的元素或示例使用回调函数的简称使用回调函数的简称使用回调函数的简称该方法类似,但其从右到左迭代的所有元素。

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

Translated by PeckZeg
Original Docs: Lodash v3.10.1 Docs

求助

翻译文档的难度比想象中的要难,特别是里面比较学术的词语,希望您再查阅的时候发现不严谨/不好/不恰当的表述或翻译的时候能斧正。

“Collection” 方法 _.at(collection, [props])

创建一个包含 collection 中相应给定的键、索引的元素数组。键必须指定为多带带的参数或键数组。

参数

collection (Array|Object|string) : 待迭代的集合

[props] (…(number|number[]|string|string[]) : 待抽取的属性名或索引(可多带带指定也可存放在一个数组中)

返回

(Array) : 返回已抽取的元素的新数组

示例

_.at(["a", "b", "c"], [0, 2]);
// → ["a", "c"]

_.at(["barney", "fred", "pebbles"], 0, 2);
// → ["barney", "pebbles"]
_.countBy(collection, [iteratee=_.identity], [thisArg])

创建一个由 collection 的每个元素经由 iteratee 生成的键值所组成的对象。每个键对应的值为 iteratee 返回的生成键的次数。iteratee 绑定 thisArg 并在执行时传入三个参数:value, index|key, collection

如果提供的是属性名,那么 predicate 将创建 _.property 风格的回调函数,并返回给定元素的属性的值。

如果值还提供了 thisArg,那么 predicate 将创建 _.matchesProperty 风格的回调,并在元素含有匹配的属性值的时候返回 true,否则返回 false

如果提供的是对象,那么 predicate 将创建 _.matches 风格的回调函数,并在匹配给定对象的属性的元素时返回 true,否则返回 false

参数

collection (Array|Object|string) : 待迭代的集合

[iteratee=_.identity] (Function|Object|string) : 每次迭代执行的函数

[thisArg] (*) : iteratee 绑定的 this

返回

(Object) : 返回yi已组成的聚合对象

示例

_.countBy([4.3, 6.1, 6.4], function(n) {
  return Math.floor(n);
});
// → { "4": 1, "6": 2 }

_.countBy([4.3, 6.1, 6.4], function(n) {
  return this.floor(n);
}, Math);
// → { "4": 1, "6": 2 }

_.countBy(["one", "two", "three"], "length");
// → { "3": 2, "5": 1 }
_.every(collection, [predicate=_.identity], [thisArg])

检查 collection每个 元素在经过 predicate 检测之后是否都返回真值。断言函数绑定 thisArg 并在执行时传入三个参数:value, index|key, collection

如果提供的是属性名,那么 predicate 将创建 _.property 风格的回调函数,并返回给定元素的属性的值。

如果值还提供了 thisArg,那么 predicate 将创建 _.matchesProperty 风格的回调,并在元素含有匹配的属性值的时候返回 true,否则返回 false

如果提供的是对象,那么 predicate 将创建 _.matches 风格的回调函数,并在匹配给定对象的属性的元素时返回 true,否则返回 false

别名

_.all

参数

collection (Array|Object|string) : 待迭代的集合

[predicate=_.identity] (Function|Object|string) : 每次迭代执行的函数

[thisArg] (*) : predicate 绑定的 this 对象

返回

(boolean) : 在所有元素都通过断言函数检查时返回 true,否则返回 false

示例

_.every([true, 1, null, "yes"], Boolean);
// → false

var users = [
  { "user": "barney", "active": false },
  { "user": "fred",   "active": false }
];

// 使用 `_.matches` 回调函数简称
_.every(users, { "user": "barney", "active": false });
// → false

// 使用 `_.matchesProperty` 回调函数简称
_.every(users, "active", false);
// → true

// 使用 `_.property` 回调函数简称
_.every(users, "active");
// → false
_.filter(collection, [predicate=_.identity], [thisArg])

迭代 collection 的每个元素,返回一个包含所有元素在传入 predicate 并返回真值的数组。断言函数绑定 thisArg 并在执行时传入三个参数:value, index|key, collection

如果提供的是属性名,那么 predicate 将创建 _.property 风格的回调函数,并返回给定元素的属性的值。

如果值还提供了 thisArg,那么 predicate 将创建 _.matchesProperty 风格的回调,并在元素含有匹配的属性值的时候返回 true,否则返回 false

如果提供的是对象,那么 predicate 将创建 _.matches 风格的回调函数,并在匹配给定对象的属性的元素时返回 true,否则返回 false

别名

_.select

参数

collection (Array|Object|string) : 待迭代的集合

[predicate=_.identity] (Function|Object|string) : 每次迭代执行时的函数

[thisArg] (*) : predicate 绑定的 this

返回

(Array) : 返回一个已过滤的新数组

示例

_.filter([4, 5, 6], function(n) {
  return n % 2 == 0;
});
// → [4, 6]

var users = [
  { "user": "barney", "age": 36, "active": true },
  { "user": "fred",   "age": 40, "active": false }
];

// 使用 `_.matches` 回调函数简称
_.pluck(_.filter(users, { "age": 36, "active": true }), "user");
// → ["barney"]

// 使用 `_.matchesProperty` 回调函数简称
_.pluck(_.filter(users, "active", false), "user");
// → ["fred"]

// 使用 `_.property` 回调函数简称
_.pluck(_.filter(users, "active"), "user");
// → ["barney"]
_.find(collection, [predicate=_.identity], [thisArg])

迭代 collection 的所有元素,返回第一个通过 predicate 并返回真值的元素。断言函数绑定 thisArg 并在执行时返回三个元素:value, index|key, collection

如果提供的是属性名,那么 predicate 将创建 _.property 风格的回调函数,并返回给定元素的属性的值。

如果值还提供了 thisArg,那么 predicate 将创建 _.matchesProperty 风格的回调,并在元素含有匹配的属性值的时候返回 true,否则返回 false

如果提供的是对象,那么 predicate 将创建 _.matches 风格的回调函数,并在匹配给定对象的属性的元素时返回 true,否则返回 false

别名

_.detect

参数

collection (Array|Object|string) : 待搜索的集合

[predicate=_.identity] (Function|Object|string) : 每次迭代执行的函数

[thisArg] (*) : predicate 绑定的 this

返回

(*) : 返回匹配的元素或 undefined

示例

var users = [
  { "user": "barney",  "age": 36, "active": true },
  { "user": "fred",    "age": 40, "active": false },
  { "user": "pebbles", "age": 1,  "active": true }
];

_.result(_.find(users, function(chr) {
  return chr.age < 40;
}), "user");
// → "barney"

// 使用 `_.matches` 回调函数的简称
_.result(_.find(users, { "age": 1, "active": true }), "user");
// → "pebbles"

// 使用 `_.matchesProperty` 回调函数的简称
_.result(_.find(users, "active", false), "user");
// → "fred"

// 使用 `_.property` 回调函数的简称
_.result(_.find(users, "active"), "user");
// → "barney"
_.findLast(collection, [predicate=_.identity], [thisArg])

该方法类似 _.find,但其从右到左迭代 collection 的所有元素。

参数

collection (Array|Object|string) : 待搜索的集合

[predicate=_.identity] (Function|Object|string) : ,每次迭代执行的函数

[thisArg] (*) : predicate 绑定的 this

返回

(*) : 返回匹配的元素或 undefined

示例

_.findLast([1, 2, 3, 4], function(n) {
  return n % 2 == 1;
});
// → 3
_.findWhere(collection, source)

collection 的每个元素与源对象执行深层次的比较,返回第一个符合属性和属性值的元素。

注意:该方法支持比较数组、布尔值、Date 对象,数值,Object 对象,正则表达式和字符串。对象将比较其拥有的属性,而非内置的、不可枚举的属性。如果要比较单个或内置属性值请查看 _.matchesProperty

参数

collection (Array|Object|string) : 待查找的集合

source (Object) : 待匹配的属性值对象

返回

_(*)_: 返回匹配的元素或 undefined

示例

var users = [
  { "user": "barney", "age": 36, "active": true },
  { "user": "fred",   "age": 40, "active": false }
];

_.result(_.findWhere(users, { "age": 36, "active": true }), "user");
// → "barney"

_.result(_.findWhere(users, { "age": 40, "active": false }), "user");
// → "fred"
_.forEach(collection, [iteratee=_.identity], [thisArg])

Iterates over elements of collection invoking iteratee for each element. The iteratee is bound to thisArg and invoked with three arguments:
(value, index|key, collection). Iteratee functions may exit iteration early by explicitly returning false.

collection 的每个元素执行 iteratee 迭代器,该迭代器将绑定 thisArg 并在执行中传入三个参数:value, index|key, collection。迭代器将在明确返回 false 时提前退出迭代。

Note: As with other "Collections" methods, objects with a "length" property are iterated like arrays. To avoid this behavior _.forIn or _.forOwn may be used for object iteration.

注意:如同和其他 "Collections" 方法,拥有“长度”属性的可迭代类数组对象,需要避免 _.forIn_.forOwn 使用在此类对象的行为。

别名

_.each

参数

collection (Array|Object|string) : 待迭代的集合

[iteratee=_.identity] (Function) : 每次迭代执行的函数

[thisArg] (*) : iteratee 绑定的 this

返回

(Array|Object|string) : 返回集合

示例

_([1, 2]).forEach(function(n) {
  console.log(n);
}).value();
// → 记录从左到右的每个值,并返回数组

_.forEach({ "a": 1, "b": 2 }, function(n, key) {
  console.log(n, key);
});
// → 记录每个 值-键 对并返回对象(不保证迭代的顺序)
_.forEachRight(collection, [iteratee=_.identity], [thisArg])

该方法类似 _.forEach,但其从右往左开始迭代 collection 的每个元素

别名

_.eachRight

参数

collection (Array|Object|string) : 待迭代的集合

[iteratee=_.identity] (Function) : 每次迭代执行的函数iteration.

[thisArg] (*) : iteratee 绑定的 this

返回

(Array|Object|string) : 返回集合

示例

_([1, 2]).forEachRight(function(n) {
  console.log(n);
}).value();
// → 从右到左记录每个值并返回数组
_.groupBy(collection, [iteratee=_.identity], [thisArg])

创建一个由 collection 的每个元素执行 iteratee 生成的键所组成的对象。每个生成的键对应的值是由每次生成该键所对应的元素所组成的数组。该迭代器绑定 thisArg 并在执行时传入三个参数:value, index|key, collection

如果提供的是属性名,那么 predicate 将创建 _.property 风格的回调函数,并返回给定元素的属性的值。

如果值还提供了 thisArg,那么 predicate 将创建 _.matchesProperty 风格的回调,并在元素含有匹配的属性值的时候返回 true,否则返回 false

如果提供的是对象,那么 predicate 将创建 _.matches 风格的回调函数,并在匹配给定对象的属性的元素时返回 true,否则返回 false

参数

collection (Array|Object|string) : 待迭代的集合

[iteratee=_.identity] (Function|Object|string) : 每次迭代执行的函数

[thisArg] (*) : iteratee 绑定的 this

返回

(Object) : 返回已组成的聚合对象

示例

_.groupBy([4.2, 6.1, 6.4], function(n) {
  return Math.floor(n);
});
// → { "4": [4.2], "6": [6.1, 6.4] }

_.groupBy([4.2, 6.1, 6.4], function(n) {
  return this.floor(n);
}, Math);
// → { "4": [4.2], "6": [6.1, 6.4] }

// 使用 `_.property` 回调函数简称
_.groupBy(["one", "two", "three"], "length");
// → { "3": ["one", "two"], "5": ["three"] }
_.includes(collection, target, [fromIndex=0])

检查 target 是否在 collection 中(使用 SameValueZero 进行相等性比较)。如果 fromIndex 为负数,则其为相对于 collection 末尾的位移。

别名

_.contains

_.include

参数

collection (Array|Object|string) : 待查找的集合

target (*) : 待查找的值

[fromIndex=0] (number) : 待查找的索引位置

返回

(boolean) : 在一个匹配元素被查找到时返回 true 否则返回 false

示例

_.includes([1, 2, 3], 1);
// → true

_.includes([1, 2, 3], 1, 2);
// → false

_.includes({ "user": "fred", "age": 40 }, "fred");
// → true

_.includes("pebbles", "eb");
// → true
_.indexBy(collection, [iteratee=_.identity], [thisArg])

Creates an object composed of keys generated from the results of running each element of collection through iteratee. The corresponding value of each key is the last element responsible for generating the key. The iteratee function is bound to thisArg and invoked with three arguments:
(value, index|key, collection).

创建一个由 collection 的每个元素执行 iteratee 生成的键所组成的对象。每个生成的键对应的值是由最后一个生成该键所对应的元素。该迭代器绑定 thisArg 并在执行时传入三个参数:value, index|key, collection

如果提供的是属性名,那么 predicate 将创建 _.property 风格的回调函数,并返回给定元素的属性的值。

如果值还提供了 thisArg,那么 predicate 将创建 _.matchesProperty 风格的回调,并在元素含有匹配的属性值的时候返回 true,否则返回 false

如果提供的是对象,那么 predicate 将创建 _.matches 风格的回调函数,并在匹配给定对象的属性的元素时返回 true,否则返回 false

参数

collection (Array|Object|string) : 待迭代的集合

[iteratee=_.identity] (Function|Object|string) : 每次迭代执行的函数

[thisArg] (*) : iteratee 绑定的 this

返回

(Object) : 返回已组成的聚合对象。

示例

var keyData = [
  { "dir": "left", "code": 97 },
  { "dir": "right", "code": 100 }
];

_.indexBy(keyData, "dir");
// → { "left": { "dir": "left", "code": 97 }, "right": { "dir": "right", "code": 100 } }

_.indexBy(keyData, function(object) {
  return String.fromCharCode(object.code);
});
// → { "a": { "dir": "left", "code": 97 }, "d": { "dir": "right", "code": 100 } }

_.indexBy(keyData, function(object) {
  return this.fromCharCode(object.code);
}, String);
// → { "a": { "dir": "left", "code": 97 }, "d": { "dir": "right", "code": 100 } }
_.invoke(collection, path, [args])

collection 的每个元素执行位于 path 的方法。返回一个执行方法返回的结果数组。在每个方法执行的时候将传入所有额外的参数。如果 methodNamecollection 中每个元素的可调用函数,则其将绑定 this

参数

collection (Array|Object|string) : 待迭代的集合

path (Array|Function|string) : 待执行方法的路径或每次迭代的可执行函数

[args] (…*) : 方法执行时传入的参数

返回

(Array) : 返回结果数组

示例

_.invoke([[5, 1, 7], [3, 2, 1]], "sort");
// → [[1, 5, 7], [1, 2, 3]]

_.invoke([123, 456], String.prototype.split, "");
// → [["1", "2", "3"], ["4", "5", "6"]]
_.map(collection, [iteratee=_.identity], [thisArg])

创建一个由 collection 的每个元素通过 iteratee 返回的值的数组。该迭代器绑定 thisArg 并在执行时传入三个参数:value, index|key, collection

如果提供的是属性名,那么 predicate 将创建 _.property 风格的回调函数,并返回给定元素的属性的值。

如果值还提供了 thisArg,那么 predicate 将创建 _.matchesProperty 风格的回调,并在元素含有匹配的属性值的时候返回 true,否则返回 false

如果提供的是对象,那么 predicate 将创建 _.matches 风格的回调函数,并在匹配给定对象的属性的元素时返回 true,否则返回 false

许多 lodash 方法在以迭代器的身份被诸如 _.every, _.filter, _.map, _.mapValues, _.reject_.some 方法执行时会被守护。

守护方法有:

ary, callback, chunk, clone, create, curry, curryRight, drop, dropRight, every, fill, flatten, invert, max, min, parseInt, slice, sortBy, take, takeRight, template, trim, trimLeft, trimRight, trunc, random, range, sample, some, sum, uniqwords

别名

_.collect

参数

collection (Array|Object|string) : 待迭代的集合

[iteratee=_.identity] (Function|Object|string) : 每次迭代执行的函数

[thisArg] (*) : iteratee 绑定的 this

返回

(Array) : 返回已映射的新数组

示例

function timesThree(n) {
  return n * 3;
}

_.map([1, 2], timesThree);
// → [3, 6]

_.map({ "a": 1, "b": 2 }, timesThree);
// → [3, 6] (iteration order is not guaranteed)

var users = [
  { "user": "barney" },
  { "user": "fred" }
];

// 使用 `_.property` 回调函数简称
_.map(users, "user");
// → ["barney", "fred"]
_.partition(collection, [predicate=_.identity], [thisArg])

创建一个包含分成两个分组的数组,第一个分组包含执行 predicate 后返回真值的元素,同时,执行 predicate 后返回假值的元素将被包含于第二个分组之中。断言函数将绑定 thisArg 并在执行时传入三个参数:value, index|key, collection

如果提供的是属性名,那么 predicate 将创建 _.property 风格的回调函数,并返回给定元素的属性的值。

如果值还提供了 thisArg,那么 predicate 将创建 _.matchesProperty 风格的回调,并在元素含有匹配的属性值的时候返回 true,否则返回 false

如果提供的是对象,那么 predicate 将创建 _.matches 风格的回调函数,并在匹配给定对象的属性的元素时返回 true,否则返回 false

参数

collection (Array|Object|string) : 待迭代的集合

[predicate=_.identity] (Function|Object|string) : 每次迭代执行的函数

[thisArg] (*) : predicate 绑定的 this

返回

(Array) : 返回包含已分组元素的数组

示例

_.partition([1, 2, 3], function(n) {
  return n % 2;
});
// → [[1, 3], [2]]

_.partition([1.2, 2.3, 3.4], function(n) {
  return this.floor(n) % 2;
}, Math);
// → [[1.2, 3.4], [2.3]]

var users = [
  { "user": "barney",  "age": 36, "active": false },
  { "user": "fred",    "age": 40, "active": true },
  { "user": "pebbles", "age": 1,  "active": false }
];

var mapper = function(array) {
  return _.pluck(array, "user");
};

// 使用 `_.matches` 回调函数简称
_.map(_.partition(users, { "age": 1, "active": false }), mapper);
// → [["pebbles"], ["barney", "fred"]]

// 使用 `_.matchesProperty` 回调函数简称
_.map(_.partition(users, "active", false), mapper);
// → [["barney", "pebbles"], ["fred"]]

// 使用 `_.property` 回调函数简称
_.map(_.partition(users, "active"), mapper);
// → [["fred"], ["barney", "pebbles"]]
_.pluck(collection, path)

Gets the property value of path from all elements in collection.

collection 中获取所有基于 path 的属性值。

参数

collection (Array|Object|string) : 待迭代的集合

path (Array|string) : 待获取的属性路径

返回

(Array) : 返回属性值

示例

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

_.pluck(users, "user");
// → ["barney", "fred"]

var userIndex = _.indexBy(users, "user");
_.pluck(userIndex, "age");
// → [36, 40] (iteration order is not guaranteed)
_.reduce(collection, [iteratee=_.identity], [accumulator], [thisArg])

Reduces collection to a value which is the accumulated result of running each element in collection through iteratee, where each successive invocation is supplied the return value of the previous. If accumulator is not provided the first element of collection is used as the initial value. The iteratee is bound to thisArg and invoked with four arguments: (accumulator, value, index|key, collection).

缩小 collection 直至成为一个值,在 collection 中对每个元素执行iteratee 而获取的累加值结果(在每次连续调用之前需要提供返回值,如果 collection 的第一个元素没有提供 accumulator,则其可以用来作为初始值)。iteratee 绑定 thisArg 并在执行时传入四个参数:accumulator, value, index|key, collection

许多 lodash 方法在以迭代器的身份被诸如 _.reduce, _.reduceRight_.transform 方法执行时会被守护。

守护方法有:

assign, defaults, defaultsDeep, includes, merge, sortByAll, 和 sortByOrder

别名

_.foldl

_.inject

参数

collection (Array|Object|string) : 待迭代的集合

[iteratee=_.identity] (Function) : 每次执行时执行的函数

[accumulator] (*) : 初始值

[thisArg] (*) : iteratee 绑定的 this

返回

(*) : 返回累加值

示例

_.reduce([1, 2], function(total, n) {
  return total + n;
});
// → 3

_.reduce({ "a": 1, "b": 2 }, function(result, n, key) {
  result[key] = n * 3;
  return result;
}, {});
// → { "a": 3, "b": 6 } (不保证执行的顺序)
_.reduceRight(collection, [iteratee=_.identity], [accumulator], [thisArg])

该方法类似 _.reduce,但其将从右往左依次迭代 collection 的元素。

别名

_.foldr

参数

collection (Array|Object|string) : 待迭代的集合

[iteratee=_.identity] (Function) : 每次迭代执行的函数

[accumulator] (*) : 初始值

[thisArg] (*) : iteratee 绑定的 this

返回

(*) : 返回累加值

示例

var array = [[0, 1], [2, 3], [4, 5]];

_.reduceRight(array, function(flattened, other) {
  return flattened.concat(other);
}, []);
// → [4, 5, 2, 3, 0, 1]
_.reject(collection, [predicate=_.identity], [thisArg])

The opposite of _.filter; this method returns the elements of collection that predicate does not return truthy for.

_.filter 作用相反,该方法返回 collection 的执行 predicate没有 返回真值的元素。

参数

collection (Array|Object|string) : 待迭代的集合

[predicate=_.identity] (Function|Object|string) : 每次迭代执行的函数

[thisArg] (*) : predicate 绑定的 this

返回

(Array) : 返回已过虑的新数组

示例

_.reject([1, 2, 3, 4], function(n) {
  return n % 2 == 0;
});
// → [1, 3]

var users = [
  { "user": "barney", "age": 36, "active": false },
  { "user": "fred",   "age": 40, "active": true }
];

// 使用 `_.matches` 回调函数简称
_.pluck(_.reject(users, { "age": 40, "active": true }), "user");
// → ["barney"]

// 使用 `_.matchesProperty` 回调函数简称
_.pluck(_.reject(users, "active", false), "user");
// → ["fred"]

// 使用 `_.property` 回调函数简称
_.pluck(_.reject(users, "active"), "user");
// → ["barney"]
_.sample(collection, [n])

从集合中随机获取一个或 n 个元素。

参数

collection (Array|Object|string) : 待取样的集合

[n] (number) : 取出样本元素的数量

返回

(*) : 返回随机的样本(们)。

示例

_.sample([1, 2, 3, 4]);
// → 2

_.sample([1, 2, 3, 4], 2);
// → [3, 1]
_.shuffle(collection)

Creates an array of shuffled values, using a version of the Fisher-Yates shuffle.

创建一个经 Fisher-Yates 洗牌算法 计算后的数组。

参数

collection (Array|Object|string) : 待洗牌的集合

返回

(Array) : 返回洗牌后的新数组

示例

_.shuffle([1, 2, 3, 4]);
// → [4, 1, 3, 2]
_.size(collection)

返回 collection 的长度,返回类数组对象的 length 值,或对象的自有可枚举属性的个数。

参数

collection (Array|Object|string) : 待检查的集合

返回

(number) : 返回 collection 的长度

示例

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

_.size({ "a": 1, "b": 2 });
// → 2

_.size("pebbles");
// → 7
_.some(collection, [predicate=_.identity], [thisArg])

只要 collection一个 元素通过 predicate 检查就返回真值。该函数在找到通过的值后立即返回,所有并不会迭代整个集合。该断言函数绑定 thisArg 并在执行时传入三个参数:value, index|key, collection

如果提供的是属性名,那么 predicate 将创建 _.property 风格的回调函数,并返回给定元素的属性的值。

如果值还提供了 thisArg,那么 predicate 将创建 _.matchesProperty 风格的回调,并在元素含有匹配的属性值的时候返回 true,否则返回 false

如果提供的是对象,那么 predicate 将创建 _.matches 风格的回调函数,并在匹配给定对象的属性的元素时返回 true,否则返回 false

别名

_.any

参数

collection (Array|Object|string) : 待迭代的集合

[predicate=_.identity] (Function|Object|string) : 每次迭代执行的函数

[thisArg] (*) : predicate 绑定的 this

返回

(boolean) : Returns true if any element passes the predicate check, else false.

示例

_.some([null, 0, "yes", false], Boolean);
// → true

var users = [
  { "user": "barney", "active": true },
  { "user": "fred",   "active": false }
];

// 使用 `_.matches` 回调函数简称
_.some(users, { "user": "barney", "active": false });
// → false

// 使用 `_.matchesProperty` 回调函数简称
_.some(users, "active", false);
// → true

// 使用 `_.property` 回调函数简称
_.some(users, "active");
// → true
_.sortBy(collection, [iteratee=_.identity], [thisArg])

创建一个数组。对集合的每个元素执行 iteratee 后的结果进行升序整理。该方法执行的是稳定排序,即其保留了原来的排序顺序。iteratee 绑定 thisArg 并在执行时传入三个参数:value, index|key, collection

如果提供的是属性名,那么 predicate 将创建 _.property 风格的回调函数,并返回给定元素的属性的值。

如果值还提供了 thisArg,那么 predicate 将创建 _.matchesProperty 风格的回调,并在元素含有匹配的属性值的时候返回 true,否则返回 false

如果提供的是对象,那么 predicate 将创建 _.matches 风格的回调函数,并在匹配给定对象的属性的元素时返回 true,否则返回 false

参数

collection (Array|Object|string) : 待迭代的结合

[iteratee=_.identity] (Function|Object|string) : 每次迭代执行的函数

[thisArg] (*) : iteratee 绑定的 this

返回

(Array) : 返回已排序的新数组

示例

_.sortBy([1, 2, 3], function(n) {
  return Math.sin(n);
});
// → [3, 1, 2]

_.sortBy([1, 2, 3], function(n) {
  return this.sin(n);
}, Math);
// → [3, 1, 2]

var users = [
  { "user": "fred" },
  { "user": "pebbles" },
  { "user": "barney" }
];

// 使用 `_.property` 回调函数的简称
_.pluck(_.sortBy(users, "user"), "user");
// → ["barney", "fred", "pebbles"]
_.sortByAll(collection, iteratees)

该方法类似 _.sortBy,但其能使用多个迭代器或属性名。

如果属性名被提供给迭代器,则其将创建 _.property 风格的回调函数,并返回给定元素的属性值。

如果对象被提供给迭代器,则其将创建_.matches 风格的回调函数,并在匹配给定对象的属性的元素时返回 true,否则返回 false

参数

collection (Array|Object|string) : 待迭代的集合

iteratees (…(Function|Function[]|Object|Object[]|string|string[]) : 排序迭代器,可指定为多个多带带的迭代器或一个迭代器数组

返回

(Array) : 返回已排序的新数组

示例

var users = [
  { "user": "fred",   "age": 48 },
  { "user": "barney", "age": 36 },
  { "user": "fred",   "age": 42 },
  { "user": "barney", "age": 34 }
];

_.map(_.sortByAll(users, ["user", "age"]), _.values);
// → [["barney", 34], ["barney", 36], ["fred", 42], ["fred", 48]]

_.map(_.sortByAll(users, "user", function(chr) {
  return Math.floor(chr.age / 10);
}), _.values);
// → [["barney", 36], ["barney", 34], ["fred", 48], ["fred", 42]]
_.sortByOrder(collection, iteratees, [orders])

该方法类似 _.sortByAll,但其允许指定迭代器排序的方式。如果未指定 orders,则所有值使用升序排列。此外,asc 表示升序,desc 则表示降序。

如果属性名被提供给迭代器,则其将创建 _.property 风格的回调函数,并返回给定元素的属性值。

如果对象被提供给迭代器,则其将创建_.matches 风格的回调函数,并在匹配给定对象的属性的元素时返回 true,否则返回 false

参数

collection (Array|Object|string) : 待迭代的集合

iteratees (Function[]|Object[]|string[]) : 排序的迭代器(们)

[orders] (string[]) : 迭代器的排序方向

返回

(Array) : 返回已排序的新数组

示例

var users = [
  { "user": "fred",   "age": 48 },
  { "user": "barney", "age": 34 },
  { "user": "fred",   "age": 42 },
  { "user": "barney", "age": 36 }
];

// sort by `user` in ascending order and by `age` in descending order
_.map(_.sortByOrder(users, ["user", "age"], ["asc", "desc"]), _.values);
// → [["barney", 36], ["barney", 34], ["fred", 48], ["fred", 42]]
_.where(collection, source)

collectionsource 的每个元素间进行深度比较。返回一个包含两边都具有相同属性值的元素的数组。

注意:该方法支持比较数组、布尔值、Date 对象、数值、Object 对象,正则表达式和字符串。对象将比较其自有而非内置、可枚举的属性。如果需要比较单个自有或内置属性值请参见 _.matchesProperty.

参数

collection (Array|Object|string) : 待查找的集合

source (Object) : 带匹配的属性值对象。

返回

(Array) : 返回已过虑的新数组

示例

var users = [
  { "user": "barney", "age": 36, "active": false, "pets": ["hoppy"] },
  { "user": "fred",   "age": 40, "active": true, "pets": ["baby puss", "dino"] }
];

_.pluck(_.where(users, { "age": 36, "active": false }), "user");
// → ["barney"]

_.pluck(_.where(users, { "pets": ["dino"] }), "user");
// → ["fred"]

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/78429.html

相关文章

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

    摘要:中文文档方法方法将两个数相加。如果提供了迭代器函数,那么其将被作用于中的每个值以来生成值排序的标准。如果提供的是对象,那么将创建风格的回调函数,并在匹配给定对象的属性的元素时返回,否则返回。 Lodash 中文文档 (v3.10.1) - Math 方法 Translated by PeckZegOriginal Docs: Lodash v3.10.1 Docs Math 方法 _....

    tianren124 评论0 收藏0
  • Lodash 中文文档 (v3.10.1) - “Number” 方法

    摘要:中文文档方法方法检查是否位于和之间包含,但不包含。参数待检查的数值起始查询范围查询范围的结束位返回在范围内时返回,否则返回示例从到包括中产生一个随机数。可能的最小值可能的最大值指定返回一个浮点数值返回一个随机数到间的浮点数 Lodash 中文文档 (v3.10.1) - Number 方法 Translated by PeckZegOriginal Docs: Lodash v3.10...

    DataPipeline 评论0 收藏0
  • Lodash 中文文档 (v3.10.1) - “Chain” 方法

    摘要:中文文档方法方法创建一个包含的对象以开启内置的方法链。注意该方法会修改包装数组。返回返回强制转为字符串的值示例执行方法链队列并提取未包装的值别名返回返回已处理的未包装的值示例 Lodash 中文文档 (v3.10.1) - Chain 方法 Translated by PeckZegOriginal Docs: Lodash v3.10.1 Docs Chain 方法 _(value)...

    BLUE 评论0 收藏0
  • Lodash 中文文档 (v3.10.1) - Array 方法

    摘要:参数待检查的已整理过的数组待计算的值每次迭代执行的函数绑定的返回返回在中应该插入的索引位置示例使用迭代器函数使用回调函数简称该方法类似,但其返回的是插入的最大的索引位置。 Lodash 中文文档 (v3.10.1) - Array 方法 Translated by PeckZegOriginal Docs: Lodash v3.10.1 Docs 更新日志 2015-01-02 感谢 ...

    史占广 评论0 收藏0
  • Lodash 中文文档 (v3.10.1) - “Function” 方法

    摘要:参数待科里化的函数函数的数量返回返回科里化的新函数示例使用占位符该方法类似但其添加的行为由变更为的值,在整体构建中的默认值是,可以作为部分参数的占位符传入。在执行时绑定的将是缓存器函数。注意缓存器函数的缓存需要暴露缓存属性,其产物会覆盖。 Lodash 中文文档 (v3.10.1) - Function 方法 Translated by PeckZegOriginal Docs: Lo...

    iKcamp 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<