減少重復操作,提升可讀性,提升團隊性。

0 _.last(array)
獲取array中的最后一個元素 array.length - 1可還行
_.head(array) 獲取數(shù)組 array 的第一個元素。
1 _.pull(array, [values])
移除數(shù)組array中所有和給定值相等的元素
indexOf、splice可還行
var array = [1, 2, 3, 1, 2, 3];
_.pull(array, 2, 3);
console.log(array);
// => [1, 1]
_.pullAt 根據(jù)索引 indexes,移除array中對應(yīng)的元素,并返回被移除元素的數(shù)組。
_.pullAll 這個方法類似 _.pull,區(qū)別是這個方法接收一個要移除值的數(shù)組。
3 _.uniq(array)
創(chuàng)建一個去重后的array數(shù)組副本。使用了 SameValueZero 做等值比較。只有第一次出現(xiàn)的元素才會被保留。
_.uniq([2, 1, 2]);
// => [2, 1]
Aarray 系列中還有扁平化、排序、交集、并集、補集等操作
4 _.forEach(collection, [iteratee=_.identity])
原生forEach 不支持對象,不能退出,這些都幫你解決啦。
還有其他高階函數(shù)哦
_.every、_.filter、_.find、_.findLast
_.forEachRight等
5 _.keyBy(collection, [iteratee=_.identity])
將數(shù)據(jù)轉(zhuǎn)化為id可以key值得對象,便于查詢,太常用了。
var list = [{ age: 12, id: 1 }, { age: 14, id: 5 }];
console.log(_.keyBy(list, (argItem) => argItem.id));

_.countBy、_.groupBy、_.orderBy、_.sortBy
6 _.shuffle(collection)
打亂一個集合
sort(()=>Math.random()-0.5) 可還行。
_.sample 從collection(集合)中獲得一個隨機元素。
_.sampleSize 從collection(集合)中獲得 n 個隨機元素。

7 _.debounce(func, [wait=0], [options={}])
防抖 setTimeout、clearTimeout可還行。
如果用手指一直按住一個彈簧,它將不會彈起直到你松手為止。
也就是說當調(diào)用動作n毫秒后,才會執(zhí)行該動作,若在這n毫秒內(nèi)又調(diào)用此動作則將重新計算執(zhí)行時間。
8 _.throttle(func, [wait=0], [options={}])
節(jié)流,限制resize這種再好不過了
將水龍頭擰緊直到水是以水滴的形式流出,那你會發(fā)現(xiàn)每隔一段時間,就會有一滴水流出。
也就是會說預(yù)先設(shè)定一個執(zhí)行周期,當調(diào)用動作的時刻大于等于執(zhí)行周期則執(zhí)行該動作,然后進入下一個新周期。
9 _.defer(func, [args])
推遲調(diào)用func,直到當前堆棧清理完畢。 調(diào)用時,任何附加的參數(shù)會傳給func。
_.delay(func, wait, [args]) 延遲 wait 毫秒后調(diào)用 func。 調(diào)用時,任何附加的參數(shù)會傳給func。
Function 還有處理參數(shù)的、調(diào)用次數(shù)、柯理化、緩存函數(shù)結(jié)果等等
10 _.cloneDeep(value)
JSON.parse(JSON.stringfy) 實現(xiàn)深拷貝可還行,還要考慮循環(huán)引用問題。
11_.isEqual(value, other)
深度判斷兩個對象的值是否相等,
_.isEmpty 檢查 value 是否為一個空對象,集合,映射或者set。
_.isError 檢查 value 是否是 Error
_.isElement 檢查 value 是否是可能是 DOM 元素。
12 _.ceil(number, [precision=0])
根據(jù) precision(精度) 向上舍入 number。( precision(精度)可以理解為保留幾位小數(shù)。)
_.ceil(4.006);
// => 5
_.ceil(6.004, 2);
// => 6.01
_.ceil(6040, -2);
// => 6100
_.floor(number, [precision=0]) 根據(jù) precision(精度) 向下舍入 number。( precision(精度)可以理解為保留幾位小數(shù)。)
_.round(number, [precision=0])
13 _.random([lower=0], [upper=1], [floating])
產(chǎn)生一個包括 lower 與 upper 之間的數(shù)。 如果只提供一個參數(shù)返回一個0到提供數(shù)之間的數(shù)。 如果 floating 設(shè)為 true,或者 lower 或 upper 是浮點數(shù),結(jié)果返回浮點數(shù)。
_.random(0, 5);
// => an integer between 0 and 5
_.random(5);
// => also an integer between 0 and 5
_.random(5, true);
// => a floating-point number between 0 and 5
_.random(1.2, 5.2);
// => a floating-point number between 1.2 and 5.2
14 _.get(object, path, [defaultValue])
當為了防止后臺返回數(shù)據(jù)存在深層次屬性取值失敗而導致的渲染問題,再也不用各種? : 、各種||,或是各種assign和擴展運算符了?。?!

var object = { 'a': [{ 'b': { 'c': 3 } }] };
_.get(object, 'a[0].b.c');
// => 3
_.get(object, ['a', '0', 'b', 'c']);
// => 3
_.get(object, 'a.b.c', 'default');
// => 'default'
_.set 設(shè)置 object對象中對應(yīng) path 屬性路徑上的值,如果path不存在,則創(chuàng)建。 缺少的索引屬性會創(chuàng)建為數(shù)組,而缺少的屬性會創(chuàng)建為對象
15 _.attempt
優(yōu)雅的寫 try catch
嘗試調(diào)用func,返回結(jié)果 或者 捕捉錯誤對象。任何附加的參數(shù)都會在調(diào)用時傳給func。
// Avoid throwing errors for invalid selectors.
var elements = _.attempt(function(selector) {
return document.querySelectorAll(selector);
}, '>_>');
if (_.isError(elements)) {
elements = [];
}
16 _.mapKeys(object, [iteratee=_.identity])
_.mapKeys({ 'a': 1, 'b': 2 }, function(value, key) {
return key + value;
});
// => { 'a1': 1, 'b2': 2 }
17 _.mapValues(object, [iteratee=_.identity])
var users = {
'fred': { 'user': 'fred', 'age': 40 },
'pebbles': { 'user': 'pebbles', 'age': 1 }
};
_.mapValues(users, function(o) { return o.age; });
// => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
// The `_.property` iteratee shorthand.
_.mapValues(users, 'age');
// => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
18 _.escape([string=''])
轉(zhuǎn)義string中的 "&", "<", ">", '"', "'", 和 "`" 字符為HTML實體字符。
_.escapeRegExp([string='']) 轉(zhuǎn)義 RegExp 字符串中特殊的字符 "^", "$", "", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", 和 "|" in .
19 _.times(n, [iteratee=_.identity])
調(diào)用 iteratee n 次,每次調(diào)用返回的結(jié)果存入到數(shù)組中。 iteratee 調(diào)用入1個參數(shù): (index)。
_.times(3, String);
// => ['0', '1', '2']
_.times(4, _.constant(0));
// => [0, 0, 0, 0]
20 _.uniqueId([prefix=''])
生成唯一ID。 如果提供了 prefix ,會被添加到ID前綴上。
_.uniqueId('contact_');
// => 'contact_104'
_.uniqueId();
// => '105'
至于鏈式調(diào)用,還是少用的好。