1. _.map(collections,function)
function square(n) {
return n * n;
}
_.map([4, 8], square);
// => [16, 64]
2._.pick從某個對象中選擇部分屬性組成新的對象
var objA = {"name": "colin", "car": "suzuki", "age": 17};
var objB = _.pick(objA, ['car', 'age']);
/{"car": "suzuki", "age": 17}
3._.find(collection, [predicate=_.identity])
遍歷集合中的元素,返回最先經 predicate
檢查爲真值的元素。 返回第一個匹配到的元素或對象
var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true } ]; _.find(users, function(o) { return o.age < 40; });
4. _.isEmpty(value) 檢查值是否是一個空對象
_.isEmpty(null);
// => true
_.isEmpty(true);
// => true
_.isEmpty(1); 如果傳入數字,最後會返回 [].length === 0。數字被當成了
// => true
object object 來判斷。
文檔裏說了傳入的是可枚舉的對象。
5._.groupBy(collection, [iteratee=_.identity])
返回一個組成彙總的對象
_.groupBy([6.1, 4.2, 6.3], Math.floor);
// => { '4': [4.2], '6': [6.1, 6.3] }
// 使用了 `_.property` 的回調結果
_.groupBy(['one', 'two', 'three'], 'length');
// => { '3': ['one', 'two'], '5': ['three'] }
6._.includes(collection,value,[fromindex=0]
檢查 value
(值) 是否在 collection
(集合) 中。如果 collection
(集合)是一個字符串,那麼檢查 value
(值,子字符串) 是否在字符串中 如果指定 fromIndex
是負數,那麼從 collection
(集合) 的結尾開始檢索
collection
(Array|Object|string): 要檢索的集合。value
(*): 要檢索的值。[fromIndex=0]
(number): 要檢索的 索引位置。
_.orderBy排序
_.orderBy(_.filter(results, item => !_.isEmpty(item) && item.state === 1), ['createTime'], ['desc'])
先過濾掉results不等於空的對象和狀態爲1的在根據創建時間排序
8._.random 在指定範圍內獲取一個隨機值
_.random(20); // 返回 0 到 20的隨機數
_.random(15, 20, true); // 返回 15 到 20的浮點型隨機數;