成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

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

張利勇 / 3590人閱讀

摘要:別名參數(shù)待搜索的集合每次迭代執(zhí)行的函數(shù)綁定的返回返回匹配的元素或示例使用回調(diào)函數(shù)的簡(jiǎn)稱使用回調(diào)函數(shù)的簡(jiǎn)稱使用回調(diào)函數(shù)的簡(jiǎn)稱該方法類似,但其從右到左迭代的所有元素。

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

Translated by PeckZeg
Original Docs: Lodash v3.10.1 Docs

求助

翻譯文檔的難度比想象中的要難,特別是里面比較學(xué)術(shù)的詞語,希望您再查閱的時(shí)候發(fā)現(xiàn)不嚴(yán)謹(jǐn)/不好/不恰當(dāng)?shù)谋硎龌蚍g的時(shí)候能斧正。

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

創(chuàng)建一個(gè)包含 collection 中相應(yīng)給定的鍵、索引的元素?cái)?shù)組。鍵必須指定為多帶帶的參數(shù)或鍵數(shù)組。

參數(shù)

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

[props] (…(number|number[]|string|string[]) : 待抽取的屬性名或索引(可多帶帶指定也可存放在一個(gè)數(shù)組中)

返回

(Array) : 返回已抽取的元素的新數(shù)組

示例

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

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

創(chuàng)建一個(gè)由 collection 的每個(gè)元素經(jīng)由 iteratee 生成的鍵值所組成的對(duì)象。每個(gè)鍵對(duì)應(yīng)的值為 iteratee 返回的生成鍵的次數(shù)。iteratee 綁定 thisArg 并在執(zhí)行時(shí)傳入三個(gè)參數(shù):value, index|key, collection。

如果提供的是屬性名,那么 predicate 將創(chuàng)建 _.property 風(fēng)格的回調(diào)函數(shù),并返回給定元素的屬性的值。

如果值還提供了 thisArg,那么 predicate 將創(chuàng)建 _.matchesProperty 風(fēng)格的回調(diào),并在元素含有匹配的屬性值的時(shí)候返回 true,否則返回 false

如果提供的是對(duì)象,那么 predicate 將創(chuàng)建 _.matches 風(fēng)格的回調(diào)函數(shù),并在匹配給定對(duì)象的屬性的元素時(shí)返回 true,否則返回 false。

參數(shù)

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

[iteratee=_.identity] (Function|Object|string) : 每次迭代執(zhí)行的函數(shù)

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

返回

(Object) : 返回yi已組成的聚合對(duì)象

示例

_.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每個(gè) 元素在經(jīng)過 predicate 檢測(cè)之后是否都返回真值。斷言函數(shù)綁定 thisArg 并在執(zhí)行時(shí)傳入三個(gè)參數(shù):value, index|key, collection

如果提供的是屬性名,那么 predicate 將創(chuàng)建 _.property 風(fēng)格的回調(diào)函數(shù),并返回給定元素的屬性的值。

如果值還提供了 thisArg,那么 predicate 將創(chuàng)建 _.matchesProperty 風(fēng)格的回調(diào),并在元素含有匹配的屬性值的時(shí)候返回 true,否則返回 false。

如果提供的是對(duì)象,那么 predicate 將創(chuàng)建 _.matches 風(fēng)格的回調(diào)函數(shù),并在匹配給定對(duì)象的屬性的元素時(shí)返回 true,否則返回 false。

別名

_.all

參數(shù)

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

[predicate=_.identity] (Function|Object|string) : 每次迭代執(zhí)行的函數(shù)

[thisArg] (*) : predicate 綁定的 this 對(duì)象

返回

(boolean) : 在所有元素都通過斷言函數(shù)檢查時(shí)返回 true,否則返回 false

示例

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

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

// 使用 `_.matches` 回調(diào)函數(shù)簡(jiǎn)稱
_.every(users, { "user": "barney", "active": false });
// → false

// 使用 `_.matchesProperty` 回調(diào)函數(shù)簡(jiǎn)稱
_.every(users, "active", false);
// → true

// 使用 `_.property` 回調(diào)函數(shù)簡(jiǎn)稱
_.every(users, "active");
// → false
_.filter(collection, [predicate=_.identity], [thisArg])

迭代 collection 的每個(gè)元素,返回一個(gè)包含所有元素在傳入 predicate 并返回真值的數(shù)組。斷言函數(shù)綁定 thisArg 并在執(zhí)行時(shí)傳入三個(gè)參數(shù):value, index|key, collection

如果提供的是屬性名,那么 predicate 將創(chuàng)建 _.property 風(fēng)格的回調(diào)函數(shù),并返回給定元素的屬性的值。

如果值還提供了 thisArg,那么 predicate 將創(chuàng)建 _.matchesProperty 風(fēng)格的回調(diào),并在元素含有匹配的屬性值的時(shí)候返回 true,否則返回 false。

如果提供的是對(duì)象,那么 predicate 將創(chuàng)建 _.matches 風(fēng)格的回調(diào)函數(shù),并在匹配給定對(duì)象的屬性的元素時(shí)返回 true,否則返回 false。

別名

_.select

參數(shù)

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

[predicate=_.identity] (Function|Object|string) : 每次迭代執(zhí)行時(shí)的函數(shù)

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

返回

(Array) : 返回一個(gè)已過濾的新數(shù)組

示例

_.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` 回調(diào)函數(shù)簡(jiǎn)稱
_.pluck(_.filter(users, { "age": 36, "active": true }), "user");
// → ["barney"]

// 使用 `_.matchesProperty` 回調(diào)函數(shù)簡(jiǎn)稱
_.pluck(_.filter(users, "active", false), "user");
// → ["fred"]

// 使用 `_.property` 回調(diào)函數(shù)簡(jiǎn)稱
_.pluck(_.filter(users, "active"), "user");
// → ["barney"]
_.find(collection, [predicate=_.identity], [thisArg])

迭代 collection 的所有元素,返回第一個(gè)通過 predicate 并返回真值的元素。斷言函數(shù)綁定 thisArg 并在執(zhí)行時(shí)返回三個(gè)元素:value, index|key, collection。

如果提供的是屬性名,那么 predicate 將創(chuàng)建 _.property 風(fēng)格的回調(diào)函數(shù),并返回給定元素的屬性的值。

如果值還提供了 thisArg,那么 predicate 將創(chuàng)建 _.matchesProperty 風(fēng)格的回調(diào),并在元素含有匹配的屬性值的時(shí)候返回 true,否則返回 false。

如果提供的是對(duì)象,那么 predicate 將創(chuàng)建 _.matches 風(fēng)格的回調(diào)函數(shù),并在匹配給定對(duì)象的屬性的元素時(shí)返回 true,否則返回 false。

別名

_.detect

參數(shù)

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

[predicate=_.identity] (Function|Object|string) : 每次迭代執(zhí)行的函數(shù)

[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` 回調(diào)函數(shù)的簡(jiǎn)稱
_.result(_.find(users, { "age": 1, "active": true }), "user");
// → "pebbles"

// 使用 `_.matchesProperty` 回調(diào)函數(shù)的簡(jiǎn)稱
_.result(_.find(users, "active", false), "user");
// → "fred"

// 使用 `_.property` 回調(diào)函數(shù)的簡(jiǎn)稱
_.result(_.find(users, "active"), "user");
// → "barney"
_.findLast(collection, [predicate=_.identity], [thisArg])

該方法類似 _.find,但其從右到左迭代 collection 的所有元素。

參數(shù)

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

[predicate=_.identity] (Function|Object|string) : ,每次迭代執(zhí)行的函數(shù)

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

返回

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

示例

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

對(duì) collection 的每個(gè)元素與源對(duì)象執(zhí)行深層次的比較,返回第一個(gè)符合屬性和屬性值的元素。

注意:該方法支持比較數(shù)組、布爾值、Date 對(duì)象,數(shù)值,Object 對(duì)象,正則表達(dá)式和字符串。對(duì)象將比較其擁有的屬性,而非內(nèi)置的、不可枚舉的屬性。如果要比較單個(gè)或內(nèi)置屬性值請(qǐng)查看 _.matchesProperty。

參數(shù)

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

source (Object) : 待匹配的屬性值對(duì)象

返回

_(*)_: 返回匹配的元素或 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.

對(duì) collection 的每個(gè)元素執(zhí)行 iteratee 迭代器,該迭代器將綁定 thisArg 并在執(zhí)行中傳入三個(gè)參數(shù):value, index|key, collection。迭代器將在明確返回 false 時(shí)提前退出迭代。

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" 方法,擁有“長(zhǎng)度”屬性的可迭代類數(shù)組對(duì)象,需要避免 _.forIn_.forOwn 使用在此類對(duì)象的行為。

別名

_.each

參數(shù)

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

[iteratee=_.identity] (Function) : 每次迭代執(zhí)行的函數(shù)

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

返回

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

示例

_([1, 2]).forEach(function(n) {
  console.log(n);
}).value();
// → 記錄從左到右的每個(gè)值,并返回?cái)?shù)組

_.forEach({ "a": 1, "b": 2 }, function(n, key) {
  console.log(n, key);
});
// → 記錄每個(gè) 值-鍵 對(duì)并返回對(duì)象(不保證迭代的順序)
_.forEachRight(collection, [iteratee=_.identity], [thisArg])

該方法類似 _.forEach,但其從右往左開始迭代 collection 的每個(gè)元素

別名

_.eachRight

參數(shù)

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

[iteratee=_.identity] (Function) : 每次迭代執(zhí)行的函數(shù)iteration.

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

返回

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

示例

_([1, 2]).forEachRight(function(n) {
  console.log(n);
}).value();
// → 從右到左記錄每個(gè)值并返回?cái)?shù)組
_.groupBy(collection, [iteratee=_.identity], [thisArg])

創(chuàng)建一個(gè)由 collection 的每個(gè)元素執(zhí)行 iteratee 生成的鍵所組成的對(duì)象。每個(gè)生成的鍵對(duì)應(yīng)的值是由每次生成該鍵所對(duì)應(yīng)的元素所組成的數(shù)組。該迭代器綁定 thisArg 并在執(zhí)行時(shí)傳入三個(gè)參數(shù):value, index|key, collection

如果提供的是屬性名,那么 predicate 將創(chuàng)建 _.property 風(fēng)格的回調(diào)函數(shù),并返回給定元素的屬性的值。

如果值還提供了 thisArg,那么 predicate 將創(chuàng)建 _.matchesProperty 風(fēng)格的回調(diào),并在元素含有匹配的屬性值的時(shí)候返回 true,否則返回 false。

如果提供的是對(duì)象,那么 predicate 將創(chuàng)建 _.matches 風(fēng)格的回調(diào)函數(shù),并在匹配給定對(duì)象的屬性的元素時(shí)返回 true,否則返回 false

參數(shù)

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

[iteratee=_.identity] (Function|Object|string) : 每次迭代執(zhí)行的函數(shù)

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

返回

(Object) : 返回已組成的聚合對(duì)象

示例

_.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` 回調(diào)函數(shù)簡(jiǎn)稱
_.groupBy(["one", "two", "three"], "length");
// → { "3": ["one", "two"], "5": ["three"] }
_.includes(collection, target, [fromIndex=0])

檢查 target 是否在 collection 中(使用 SameValueZero 進(jìn)行相等性比較)。如果 fromIndex 為負(fù)數(shù),則其為相對(duì)于 collection 末尾的位移。

別名

_.contains

_.include

參數(shù)

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

target (*) : 待查找的值

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

返回

(boolean) : 在一個(gè)匹配元素被查找到時(shí)返回 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).

創(chuàng)建一個(gè)由 collection 的每個(gè)元素執(zhí)行 iteratee 生成的鍵所組成的對(duì)象。每個(gè)生成的鍵對(duì)應(yīng)的值是由最后一個(gè)生成該鍵所對(duì)應(yīng)的元素。該迭代器綁定 thisArg 并在執(zhí)行時(shí)傳入三個(gè)參數(shù):value, index|key, collection

如果提供的是屬性名,那么 predicate 將創(chuàng)建 _.property 風(fēng)格的回調(diào)函數(shù),并返回給定元素的屬性的值。

如果值還提供了 thisArg,那么 predicate 將創(chuàng)建 _.matchesProperty 風(fēng)格的回調(diào),并在元素含有匹配的屬性值的時(shí)候返回 true,否則返回 false。

如果提供的是對(duì)象,那么 predicate 將創(chuàng)建 _.matches 風(fēng)格的回調(diào)函數(shù),并在匹配給定對(duì)象的屬性的元素時(shí)返回 true,否則返回 false

參數(shù)

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

[iteratee=_.identity] (Function|Object|string) : 每次迭代執(zhí)行的函數(shù)

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

返回

(Object) : 返回已組成的聚合對(duì)象。

示例

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])

對(duì) collection 的每個(gè)元素執(zhí)行位于 path 的方法。返回一個(gè)執(zhí)行方法返回的結(jié)果數(shù)組。在每個(gè)方法執(zhí)行的時(shí)候?qū)魅胨蓄~外的參數(shù)。如果 methodNamecollection 中每個(gè)元素的可調(diào)用函數(shù),則其將綁定 this。

參數(shù)

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

path (Array|Function|string) : 待執(zhí)行方法的路徑或每次迭代的可執(zhí)行函數(shù)

[args] (…*) : 方法執(zhí)行時(shí)傳入的參數(shù)

返回

(Array) : 返回結(jié)果數(shù)組

示例

_.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])

創(chuàng)建一個(gè)由 collection 的每個(gè)元素通過 iteratee 返回的值的數(shù)組。該迭代器綁定 thisArg 并在執(zhí)行時(shí)傳入三個(gè)參數(shù):value, index|key, collection。

如果提供的是屬性名,那么 predicate 將創(chuàng)建 _.property 風(fēng)格的回調(diào)函數(shù),并返回給定元素的屬性的值。

如果值還提供了 thisArg,那么 predicate 將創(chuàng)建 _.matchesProperty 風(fēng)格的回調(diào),并在元素含有匹配的屬性值的時(shí)候返回 true,否則返回 false。

如果提供的是對(duì)象,那么 predicate 將創(chuàng)建 _.matches 風(fēng)格的回調(diào)函數(shù),并在匹配給定對(duì)象的屬性的元素時(shí)返回 true,否則返回 false。

許多 lodash 方法在以迭代器的身份被諸如 _.every, _.filter, _.map, _.mapValues, _.reject_.some 方法執(zhí)行時(shí)會(huì)被守護(hù)。

守護(hù)方法有:

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

參數(shù)

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

[iteratee=_.identity] (Function|Object|string) : 每次迭代執(zhí)行的函數(shù)

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

返回

(Array) : 返回已映射的新數(shù)組

示例

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` 回調(diào)函數(shù)簡(jiǎn)稱
_.map(users, "user");
// → ["barney", "fred"]
_.partition(collection, [predicate=_.identity], [thisArg])

創(chuàng)建一個(gè)包含分成兩個(gè)分組的數(shù)組,第一個(gè)分組包含執(zhí)行 predicate 后返回真值的元素,同時(shí),執(zhí)行 predicate 后返回假值的元素將被包含于第二個(gè)分組之中。斷言函數(shù)將綁定 thisArg 并在執(zhí)行時(shí)傳入三個(gè)參數(shù):value, index|key, collection。

如果提供的是屬性名,那么 predicate 將創(chuàng)建 _.property 風(fēng)格的回調(diào)函數(shù),并返回給定元素的屬性的值。

如果值還提供了 thisArg,那么 predicate 將創(chuàng)建 _.matchesProperty 風(fēng)格的回調(diào),并在元素含有匹配的屬性值的時(shí)候返回 true,否則返回 false。

如果提供的是對(duì)象,那么 predicate 將創(chuàng)建 _.matches 風(fēng)格的回調(diào)函數(shù),并在匹配給定對(duì)象的屬性的元素時(shí)返回 true,否則返回 false。

參數(shù)

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

[predicate=_.identity] (Function|Object|string) : 每次迭代執(zhí)行的函數(shù)

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

返回

(Array) : 返回包含已分組元素的數(shù)組

示例

_.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` 回調(diào)函數(shù)簡(jiǎn)稱
_.map(_.partition(users, { "age": 1, "active": false }), mapper);
// → [["pebbles"], ["barney", "fred"]]

// 使用 `_.matchesProperty` 回調(diào)函數(shù)簡(jiǎn)稱
_.map(_.partition(users, "active", false), mapper);
// → [["barney", "pebbles"], ["fred"]]

// 使用 `_.property` 回調(diào)函數(shù)簡(jiǎn)稱
_.map(_.partition(users, "active"), mapper);
// → [["fred"], ["barney", "pebbles"]]
_.pluck(collection, path)

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

collection 中獲取所有基于 path 的屬性值。

參數(shù)

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 直至成為一個(gè)值,在 collection 中對(duì)每個(gè)元素執(zhí)行iteratee 而獲取的累加值結(jié)果(在每次連續(xù)調(diào)用之前需要提供返回值,如果 collection 的第一個(gè)元素沒有提供 accumulator,則其可以用來作為初始值)。iteratee 綁定 thisArg 并在執(zhí)行時(shí)傳入四個(gè)參數(shù):accumulator, value, index|key, collection。

許多 lodash 方法在以迭代器的身份被諸如 _.reduce, _.reduceRight_.transform 方法執(zhí)行時(shí)會(huì)被守護(hù)。

守護(hù)方法有:

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

別名

_.foldl

_.inject

參數(shù)

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

[iteratee=_.identity] (Function) : 每次執(zhí)行時(shí)執(zhí)行的函數(shù)

[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 } (不保證執(zhí)行的順序)
_.reduceRight(collection, [iteratee=_.identity], [accumulator], [thisArg])

該方法類似 _.reduce,但其將從右往左依次迭代 collection 的元素。

別名

_.foldr

參數(shù)

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

[iteratee=_.identity] (Function) : 每次迭代執(zhí)行的函數(shù)

[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 的執(zhí)行 predicate沒有 返回真值的元素。

參數(shù)

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

[predicate=_.identity] (Function|Object|string) : 每次迭代執(zhí)行的函數(shù)

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

返回

(Array) : 返回已過慮的新數(shù)組

示例

_.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` 回調(diào)函數(shù)簡(jiǎn)稱
_.pluck(_.reject(users, { "age": 40, "active": true }), "user");
// → ["barney"]

// 使用 `_.matchesProperty` 回調(diào)函數(shù)簡(jiǎn)稱
_.pluck(_.reject(users, "active", false), "user");
// → ["fred"]

// 使用 `_.property` 回調(diào)函數(shù)簡(jiǎn)稱
_.pluck(_.reject(users, "active"), "user");
// → ["barney"]
_.sample(collection, [n])

從集合中隨機(jī)獲取一個(gè)或 n 個(gè)元素。

參數(shù)

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

[n] (number) : 取出樣本元素的數(shù)量

返回

(*) : 返回隨機(jī)的樣本(們)。

示例

_.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.

創(chuàng)建一個(gè)經(jīng) Fisher-Yates 洗牌算法 計(jì)算后的數(shù)組。

參數(shù)

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

返回

(Array) : 返回洗牌后的新數(shù)組

示例

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

返回 collection 的長(zhǎng)度,返回類數(shù)組對(duì)象的 length 值,或?qū)ο蟮淖杂锌擅杜e屬性的個(gè)數(shù)。

參數(shù)

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

返回

(number) : 返回 collection 的長(zhǎng)度

示例

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

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

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

只要 collection一個(gè) 元素通過 predicate 檢查就返回真值。該函數(shù)在找到通過的值后立即返回,所有并不會(huì)迭代整個(gè)集合。該斷言函數(shù)綁定 thisArg 并在執(zhí)行時(shí)傳入三個(gè)參數(shù):value, index|key, collection。

如果提供的是屬性名,那么 predicate 將創(chuàng)建 _.property 風(fēng)格的回調(diào)函數(shù),并返回給定元素的屬性的值。

如果值還提供了 thisArg,那么 predicate 將創(chuàng)建 _.matchesProperty 風(fēng)格的回調(diào),并在元素含有匹配的屬性值的時(shí)候返回 true,否則返回 false。

如果提供的是對(duì)象,那么 predicate 將創(chuàng)建 _.matches 風(fēng)格的回調(diào)函數(shù),并在匹配給定對(duì)象的屬性的元素時(shí)返回 true,否則返回 false

別名

_.any

參數(shù)

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

[predicate=_.identity] (Function|Object|string) : 每次迭代執(zhí)行的函數(shù)

[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` 回調(diào)函數(shù)簡(jiǎn)稱
_.some(users, { "user": "barney", "active": false });
// → false

// 使用 `_.matchesProperty` 回調(diào)函數(shù)簡(jiǎn)稱
_.some(users, "active", false);
// → true

// 使用 `_.property` 回調(diào)函數(shù)簡(jiǎn)稱
_.some(users, "active");
// → true
_.sortBy(collection, [iteratee=_.identity], [thisArg])

創(chuàng)建一個(gè)數(shù)組。對(duì)集合的每個(gè)元素執(zhí)行 iteratee 后的結(jié)果進(jìn)行升序整理。該方法執(zhí)行的是穩(wěn)定排序,即其保留了原來的排序順序。iteratee 綁定 thisArg 并在執(zhí)行時(shí)傳入三個(gè)參數(shù):value, index|key, collection。

如果提供的是屬性名,那么 predicate 將創(chuàng)建 _.property 風(fēng)格的回調(diào)函數(shù),并返回給定元素的屬性的值。

如果值還提供了 thisArg,那么 predicate 將創(chuàng)建 _.matchesProperty 風(fēng)格的回調(diào),并在元素含有匹配的屬性值的時(shí)候返回 true,否則返回 false

如果提供的是對(duì)象,那么 predicate 將創(chuàng)建 _.matches 風(fēng)格的回調(diào)函數(shù),并在匹配給定對(duì)象的屬性的元素時(shí)返回 true,否則返回 false。

參數(shù)

collection (Array|Object|string) : 待迭代的結(jié)合

[iteratee=_.identity] (Function|Object|string) : 每次迭代執(zhí)行的函數(shù)

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

返回

(Array) : 返回已排序的新數(shù)組

示例

_.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` 回調(diào)函數(shù)的簡(jiǎn)稱
_.pluck(_.sortBy(users, "user"), "user");
// → ["barney", "fred", "pebbles"]
_.sortByAll(collection, iteratees)

該方法類似 _.sortBy,但其能使用多個(gè)迭代器或?qū)傩悦?/p>

如果屬性名被提供給迭代器,則其將創(chuàng)建 _.property 風(fēng)格的回調(diào)函數(shù),并返回給定元素的屬性值。

如果對(duì)象被提供給迭代器,則其將創(chuàng)建_.matches 風(fēng)格的回調(diào)函數(shù),并在匹配給定對(duì)象的屬性的元素時(shí)返回 true,否則返回 false。

參數(shù)

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

iteratees (…(Function|Function[]|Object|Object[]|string|string[]) : 排序迭代器,可指定為多個(gè)多帶帶的迭代器或一個(gè)迭代器數(shù)組

返回

(Array) : 返回已排序的新數(shù)組

示例

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 則表示降序。

如果屬性名被提供給迭代器,則其將創(chuàng)建 _.property 風(fēng)格的回調(diào)函數(shù),并返回給定元素的屬性值。

如果對(duì)象被提供給迭代器,則其將創(chuàng)建_.matches 風(fēng)格的回調(diào)函數(shù),并在匹配給定對(duì)象的屬性的元素時(shí)返回 true,否則返回 false。

參數(shù)

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

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

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

返回

(Array) : 返回已排序的新數(shù)組

示例

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 的每個(gè)元素間進(jìn)行深度比較。返回一個(gè)包含兩邊都具有相同屬性值的元素的數(shù)組。

注意:該方法支持比較數(shù)組、布爾值、Date 對(duì)象、數(shù)值、Object 對(duì)象,正則表達(dá)式和字符串。對(duì)象將比較其自有而非內(nèi)置、可枚舉的屬性。如果需要比較單個(gè)自有或內(nèi)置屬性值請(qǐng)參見 _.matchesProperty.。

參數(shù)

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

source (Object) : 帶匹配的屬性值對(duì)象。

返回

(Array) : 返回已過慮的新數(shù)組

示例

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"]

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/78429.html

相關(guān)文章

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

    摘要:中文文檔方法方法將兩個(gè)數(shù)相加。如果提供了迭代器函數(shù),那么其將被作用于中的每個(gè)值以來生成值排序的標(biāo)準(zhǔn)。如果提供的是對(duì)象,那么將創(chuàng)建風(fēng)格的回調(diào)函數(shù),并在匹配給定對(duì)象的屬性的元素時(shí)返回,否則返回。 Lodash 中文文檔 (v3.10.1) - Math 方法 Translated by PeckZegOriginal Docs: Lodash v3.10.1 Docs Math 方法 _....

    tianren124 評(píng)論0 收藏0
  • Lodash 中文文檔 (v3.10.1) - “Number” 方法

    摘要:中文文檔方法方法檢查是否位于和之間包含,但不包含。參數(shù)待檢查的數(shù)值起始查詢范圍查詢范圍的結(jié)束位返回在范圍內(nèi)時(shí)返回,否則返回示例從到包括中產(chǎn)生一個(gè)隨機(jī)數(shù)??赡艿淖钚≈悼赡艿淖畲笾抵付ǚ祷匾粋€(gè)浮點(diǎn)數(shù)值返回一個(gè)隨機(jī)數(shù)到間的浮點(diǎn)數(shù) Lodash 中文文檔 (v3.10.1) - Number 方法 Translated by PeckZegOriginal Docs: Lodash v3.10...

    DataPipeline 評(píng)論0 收藏0
  • Lodash 中文文檔 (v3.10.1) - “Chain” 方法

    摘要:中文文檔方法方法創(chuàng)建一個(gè)包含的對(duì)象以開啟內(nèi)置的方法鏈。注意該方法會(huì)修改包裝數(shù)組。返回返回強(qiáng)制轉(zhuǎn)為字符串的值示例執(zhí)行方法鏈隊(duì)列并提取未包裝的值別名返回返回已處理的未包裝的值示例 Lodash 中文文檔 (v3.10.1) - Chain 方法 Translated by PeckZegOriginal Docs: Lodash v3.10.1 Docs Chain 方法 _(value)...

    BLUE 評(píng)論0 收藏0
  • Lodash 中文文檔 (v3.10.1) - Array 方法

    摘要:參數(shù)待檢查的已整理過的數(shù)組待計(jì)算的值每次迭代執(zhí)行的函數(shù)綁定的返回返回在中應(yīng)該插入的索引位置示例使用迭代器函數(shù)使用回調(diào)函數(shù)簡(jiǎn)稱該方法類似,但其返回的是插入的最大的索引位置。 Lodash 中文文檔 (v3.10.1) - Array 方法 Translated by PeckZegOriginal Docs: Lodash v3.10.1 Docs 更新日志 2015-01-02 感謝 ...

    史占廣 評(píng)論0 收藏0
  • Lodash 中文文檔 (v3.10.1) - “Function” 方法

    摘要:參數(shù)待科里化的函數(shù)函數(shù)的數(shù)量返回返回科里化的新函數(shù)示例使用占位符該方法類似但其添加的行為由變更為的值,在整體構(gòu)建中的默認(rèn)值是,可以作為部分參數(shù)的占位符傳入。在執(zhí)行時(shí)綁定的將是緩存器函數(shù)。注意緩存器函數(shù)的緩存需要暴露緩存屬性,其產(chǎn)物會(huì)覆蓋。 Lodash 中文文檔 (v3.10.1) - Function 方法 Translated by PeckZegOriginal Docs: Lo...

    iKcamp 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<