成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

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

張利勇 / 3628人閱讀

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

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

Translated by PeckZeg
Original Docs: Lodash v3.10.1 Docs

求助

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

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

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

參數(shù)

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

[props] (…(number|number[]|string|string[]) : 待抽取的屬性名或索引(可多帶帶指定也可存放在一個數(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)建一個由 collection 的每個元素經(jīng)由 iteratee 生成的鍵值所組成的對象。每個鍵對應的值為 iteratee 返回的生成鍵的次數(shù)。iteratee 綁定 thisArg 并在執(zhí)行時傳入三個參數(shù):value, index|key, collection。

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

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

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

參數(shù)

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

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

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

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

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

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

別名

_.all

參數(shù)

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

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

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

返回

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

示例

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

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

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

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

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

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

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

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

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

別名

_.select

參數(shù)

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

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

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

返回

(Array) : 返回一個已過濾的新數(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ù)簡稱
_.pluck(_.filter(users, { "age": 36, "active": true }), "user");
// → ["barney"]

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

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

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

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

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

如果提供的是對象,那么 predicate 將創(chuàng)建 _.matches 風格的回調(diào)函數(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ù)的簡稱
_.result(_.find(users, { "age": 1, "active": true }), "user");
// → "pebbles"

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

// 使用 `_.property` 回調(diào)函數(shù)的簡稱
_.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)

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

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

參數(shù)

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 的每個元素執(zhí)行 iteratee 迭代器,該迭代器將綁定 thisArg 并在執(zhí)行中傳入三個參數(shù):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" 方法,擁有“長度”屬性的可迭代類數(shù)組對象,需要避免 _.forIn_.forOwn 使用在此類對象的行為。

別名

_.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();
// → 記錄從左到右的每個值,并返回數(shù)組

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

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

別名

_.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();
// → 從右到左記錄每個值并返回數(shù)組
_.groupBy(collection, [iteratee=_.identity], [thisArg])

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

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

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

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

參數(shù)

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

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

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

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

別名

_.contains

_.include

參數(shù)

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

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

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

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

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

參數(shù)

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

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

[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 的每個元素執(zhí)行位于 path 的方法。返回一個執(zhí)行方法返回的結果數(shù)組。在每個方法執(zhí)行的時候將傳入所有額外的參數(shù)。如果 methodNamecollection 中每個元素的可調(diào)用函數(shù),則其將綁定 this。

參數(shù)

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

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

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

返回

(Array) : 返回結果數(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)建一個由 collection 的每個元素通過 iteratee 返回的值的數(shù)組。該迭代器綁定 thisArg 并在執(zhí)行時傳入三個參數(shù):value, index|key, collection。

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

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

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

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

守護方法有:

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ù)簡稱
_.map(users, "user");
// → ["barney", "fred"]
_.partition(collection, [predicate=_.identity], [thisArg])

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

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

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

如果提供的是對象,那么 predicate 將創(chuàng)建 _.matches 風格的回調(diào)函數(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ù)簡稱
_.map(_.partition(users, { "age": 1, "active": false }), mapper);
// → [["pebbles"], ["barney", "fred"]]

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

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

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

守護方法有:

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

別名

_.foldl

_.inject

參數(shù)

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

[iteratee=_.identity] (Function) : 每次執(zhí)行時執(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ù)簡稱
_.pluck(_.reject(users, { "age": 40, "active": true }), "user");
// → ["barney"]

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

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

從集合中隨機獲取一個或 n 個元素。

參數(shù)

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

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

返回

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

示例

_.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)建一個經(jīng) Fisher-Yates 洗牌算法 計算后的數(shù)組。

參數(shù)

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

返回

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

示例

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

返回 collection 的長度,返回類數(shù)組對象的 length 值,或對象的自有可枚舉屬性的個數(shù)。

參數(shù)

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 檢查就返回真值。該函數(shù)在找到通過的值后立即返回,所有并不會迭代整個集合。該斷言函數(shù)綁定 thisArg 并在執(zhí)行時傳入三個參數(shù):value, index|key, collection。

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

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

如果提供的是對象,那么 predicate 將創(chuàng)建 _.matches 風格的回調(diào)函數(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ù)簡稱
_.some(users, { "user": "barney", "active": false });
// → false

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

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

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

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

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

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

參數(shù)

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

[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ù)的簡稱
_.pluck(_.sortBy(users, "user"), "user");
// → ["barney", "fred", "pebbles"]
_.sortByAll(collection, iteratees)

該方法類似 _.sortBy,但其能使用多個迭代器或屬性名。

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

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

參數(shù)

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

iteratees (…(Function|Function[]|Object|Object[]|string|string[]) : 排序迭代器,可指定為多個多帶帶的迭代器或一個迭代器數(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 風格的回調(diào)函數(shù),并返回給定元素的屬性值。

如果對象被提供給迭代器,則其將創(chuàng)建_.matches 風格的回調(diào)函數(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 的每個元素間進行深度比較。返回一個包含兩邊都具有相同屬性值的元素的數(shù)組。

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

參數(shù)

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

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

返回

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

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

轉載請注明本文地址:http://m.hztianpu.com/yun/78429.html

相關文章

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

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

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

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

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

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

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

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

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

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

    iKcamp 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<