_.at(collection, [props]) : 查找 ,根据索引或者键名来查找值,返回数组,props可以是数组,也可以是多个值 (干净)
_.at(collection, [props])
1 2345
_.at(['a', 'b', 'c'], [0, 2]);// => ['a', 'c']_.at(['barney', 'fred', 'pebbles'], 0, 2);// => ['barney', 'pebbles']
_.countBy(collection, [iteratee=_.identity], [thisArg]) : 遍历,统计 ,遍历collection,返回对象,对象中的key是iteratee处理后的返回值,值为重复的次数。 (干净)
_.countBy(collection, [iteratee=_.identity], [thisArg])
123456789101112
_.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,返回boolean (干净)
_.every(collection, [predicate=_.identity], [thisArg])
12345678910111213141516171819
_.every([true, 1, null, 'yes'], Boolean);// => falsevar users = [ { 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': false }];// using the `_.matches` callback shorthand_.every(users, { 'user': 'barney', 'active': false });// => false// using the `_.matchesProperty` callback shorthand_.every(users, 'active', false);// => true// using the `_.property` callback shorthand_.every(users, 'active');// => false(active都是false)
_.filter(collection, [predicate=_.identity], [thisArg]) : 遍历,过滤 ,遍历collection,筛选出符合predicate的元素,返回数组 (干净)
_.filter(collection, [predicate=_.identity], [thisArg])
123456789101112131415161718192021
_.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 }];// using the `_.matches` callback shorthand_.pluck(_.filter(users, { 'age': 36, 'active': true }), 'user');// => ['barney']// using the `_.matchesProperty` callback shorthand_.pluck(_.filter(users, 'active', false), 'user');// => ['fred']// using the `_.property` callback shorthand_.pluck(_.filter(users, 'active'), 'user');// => ['barney']
_.find(collection, [predicate=_.identity], [thisArg]) : 遍历,查找 ,遍历collection,查找出第一个符合predicate条件的一个元素并返回。单个元素 (干净)
_.find(collection, [predicate=_.identity], [thisArg])
12345678910111213141516171819202122
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'// using the `_.matches` callback shorthand_.result(_.find(users, { 'age': 1, 'active': true }), 'user');// => 'pebbles'// using the `_.matchesProperty` callback shorthand_.result(_.find(users, 'active', false), 'user');// => 'fred'// using the `_.property` callback shorthand_.result(_.find(users, 'active'), 'user');// => 'barney'
_.findLast(collection, [predicate=_.identity], [thisArg]) : 类似上,反向
_.findLast(collection, [predicate=_.identity], [thisArg])
_.findWhere(collection, source) : 遍历,查找 ,类似find,传参不同。 (干净)
_.findWhere(collection, source)
12345678910
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]) : 遍历 ,遍历collection,对齐每个元素做响应的iteratee处理。 (脏)
_.forEach(collection, [iteratee=_.identity], [thisArg])
123456789
_([1, 2]).forEach(function(n) { console.log(n);}).value();// => logs each value from left to right and returns the array_.forEach({ 'a': 1, 'b': 2 }, function(n, key) { console.log(n, key);});// => logs each value-key pair and returns the object (iteration order is not guaranteed)
_.forEachRight(collection, [iteratee=_.identity], [thisArg]) : 类似上,反向
_.forEachRight(collection, [iteratee=_.identity], [thisArg])
_.groupBy(collection, [iteratee=_.identity], [thisArg]) : 分组 ,将collection根据iteratee进行分组。 (干净)
_.groupBy(collection, [iteratee=_.identity], [thisArg])
12345678910111213
_.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] }// using the `_.property` callback shorthand_.groupBy(['one', 'two', 'three'], 'length');// => { '3': ['one', 'two'], '5': ['three'] }
_.includes(collection, target, [fromIndex=0]) : 判断 ,判断collection中是否包含target。 (干净)
_.includes(collection, target, [fromIndex=0])
1234567 891011121314
_.includes([1, 2, 3], 1);// => true_.includes([1, 2, 3], 1, 2);// => false_.includes({ 'user': 'fred', 'age': 40 }, 'fred');// => true_.includes({ 'user': 'fred', 'age': 40 }, 'user');// => false_.includes('pebbles', 'eb');// => true
_.indexBy(collection, [iteratee=_.identity], [thisArg]) : 遍历,倒置 ,遍历collention,返回对象,通过iteratee来获取指向元素的key,重复的key,后者覆盖前者 (干净)
_.indexBy(collection, [iteratee=_.identity], [thisArg])
1234567891011121314151617181920212223242526
var keyData = [ { 'dir': 'left', 'code': 97 }, { 'dir': 'right', 'code': 100 }, { 'dir': 'left', 'code': 101 }];_.indexBy(keyData, 'dir');// => { 'left': { 'dir': 'left', 'code': 101 }, 'right': { 'dir': 'right', 'code': 100 } }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提供的方法来处理,返回数组 (干净)
_.invoke(collection, path, [args])
12345
_.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]) : 遍历,取值 遍历数组获取需要的值 (干净)
_.map(collection, [iteratee=_.identity],[thisArg])
123456789101112131415161718
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' }];// using the `_.property` callback shorthand_.map(users, 'user');// => ['barney', 'fred']
_.partition(collection, [predicate=_.identity], [thisArg]) : 遍历,分组 遍历collection,根据predicate来确定分组条件,返回数组 (干净)
_.partition(collection, [predicate=_.identity], [thisArg])
_.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 }];_.partition(users, { 'age': 1, 'active': false })// => [[{user: "pebbles", age: 1, active: false}],[{user: "barney", age: 36, active: false},{user: "fred", age: 40, active: true}]]
_.pluck(collection, path) : 遍历,取值 遍历collection,去出path匹配的所有属性值,组成数组返回 (干净)
_.pluck(collection, path)
1234567891011
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]) : 遍历 遍历collection,返回一个积累值 (干净)
_.reduce(collection, [iteratee=_.identity], [accumulator], [thisArg])
_.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 } (iteration order is not guaranteed)
_.reduceRight(collection, [iteratee=_.identity], [accumulator], [thisArg]) : 类似上,反向
_.reduceRight(collection, [iteratee=_.identity], [accumulator], [thisArg])
_.reject(collection, [predicate=_.identity], [thisArg]) : 遍历,过滤 过滤掉满足条件的值,与_.filter相反 (干净)
_.reject(collection, [predicate=_.identity], [thisArg])
_.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 }];// using the `_.matches` callback shorthand_.pluck(_.reject(users, { 'age': 40, 'active': true }), 'user');// => ['barney']// using the `_.matchesProperty` callback shorthand_.pluck(_.reject(users, 'active', false), 'user');// => ['fred']// using the `_.property` callback shorthand_.pluck(_.reject(users, 'active'), 'user');// => ['barney']
_.sample(collection, [n]) : 随机数 从collection 随机取一个或者n个元素 (干净)
_.sample(collection, [n])
_.sample([1, 2, 3, 4]);// => 2_.sample([1, 2, 3, 4], 2);// => [3, 1]
_.shuffle(collection) : 打乱,随机 随机打乱一个collection (干净)
_.shuffle(collection)
12
_.shuffle([1, 2, 3, 4]);// => [4, 1, 3, 2]
_.size(collection) : 长度,大小 获取collection的长度 (干净)
_.size(collection)
12345678
_.size([1, 2, 3]);// => 3_.size({ 'a': 1, 'b': 2 });// => 2_.size('pebbles');// => 7
_.some(collection, [predicate=_.identity], [thisArg]) : 判断,遍历 遍历collection,判断是否存在某个元素符合predicate (干净)
_.some(collection, [predicate=_.identity], [thisArg])
_.some([null, 0, 'yes', false], Boolean);// => truevar users = [ { 'user': 'barney', 'active': true }, { 'user': 'fred', 'active': false }];// using the `_.matches` callback shorthand_.some(users, { 'user': 'barney', 'active': false });// => false// using the `_.matchesProperty` callback shorthand_.some(users, 'active', false);// => true// using the `_.property` callback shorthand_.some(users, 'active');// => true
_.sortBy(collection, [iteratee=_.identity], [thisArg]) : 排序 根据iteratee对collection进行排序, (干净)
_.sortBy(collection, [iteratee=_.identity], [thisArg])
_.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' }];// using the `_.property` callback shorthand_.pluck(_.sortBy(users, 'user'), 'user');// => ['barney', 'fred', 'pebbles']
_.sortByAll(collection, iteratees) : 排序 根据iteratees多条件对collection进行排序, (干净)
_.sortByAll(collection, iteratees)
1234567891011121314
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]) : 排序 可以控制升序还是降序 (干净)
_.sortByOrder(collection, iteratees, [orders])
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) : 遍历,过滤 返回符合条件的元素组成的数组 (干净)
_.where(collection, source)
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']
_.chunk(array, [size=1]) : 拆分 ,将 array 拆分成多个 size 长度的块,把这些块组成一个新二维数组。 如果无法被分割成全部等长的块,那么最后剩余的元素将组成一个块。 (干净)
_.chunk(array, [size=1])
_.chunk(['a', 'b', 'c', 'd'], 2);// => [['a', 'b'], ['c', 'd']]
_.compact(array) : 过滤 ,去掉假值,例如:false、null、 0、””、undefined 和 NaN (干净)
_.compact(array)
_.compact([0, 1, false, 2, '', 3]);// => [1, 2, 3]
_.difference(array, [values]) : 过滤 ,去掉array中存在于values中的元素,区分字符和数字 (干净)
_.difference(array, [values])
1234
_.difference([1, 2, 3], [4, 2]);// => [1, 3]_.difference([1, '2', 3], [4, 2]);// => [1, "2", 3]
_.pull(array, [values]) : 过滤 ,移除数组array中所有和 values 相等===的元素 (脏)
_.pull(array, [values])
var array = [1, 2, 3, 1, 2, 3];_.pull(array, 2, 3);console.log(array);// => [1, 1]
_.without(array, [values]) : 过滤 ,移除数组array中所有和 values 相等===的元素 (干净)
_.without(array, [values])
var array = [1, 2, 3, 1, 2, 3]; _.pull(array, 2, 3);console.log(array);// => [1, 1]
_.pullAt(array, [indexes]) : 过滤 ,移除指定索引的数组元素,并返回移除的元素,索引值明确给出或者是索引数组. (脏)
_.pullAt(array, [indexes])
var array = [5, 10, 15, 20];var evens = _.pullAt(array, 1, 3);console.log(array);// => [5, 15]console.log(evens);// => [10, 20]
_.remove(array, [predicate=_.identity], [thisArg]) : 过滤 ,移除数组 array 中满足 predicate 条件的所有元素 ,返回的是被移除元素数组. (脏)
_.remove(array, [predicate=_.identity], [thisArg])
var array = [1, 2, 3, 4];var evens = _.remove(array, function(n) { return n % 2 == 0;});console.log(array);// => [1, 3]console.log(evens);// => [2, 4]
_.drop(array, [n=1]) : 截取 ,去掉头部n个元素 (干净)
_.drop(array, [n=1])
_.drop([1, 2, 3], 2);// => [3]
_.dropRight(array, [n=1]) : 类似上
_.dropRight(array, [n=1])
_.dropWhile(array, [predicate=_.identity], [thisArg]) : 截取 ,从开头查询(左数起)数组 array ,第一个不满足predicate 条件的元素开始截取数组. thisArg为predicate 条件执行上下文对象绑定的参数 . [predicate=_.identity] 可以为 (Function|Object|string) (干净)
_.dropWhile(array, [predicate=_.identity], [thisArg])
_.dropWhile([1, 2, 3], function(n) { return n < 3;});// => [3]var users = [ { 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': true }];// using the `_.matches` callback shorthand_.pluck(_.dropWhile(users, { 'user': 'barney', 'active': false }), 'user');// => ['fred', 'pebbles']// using the `_.matchesProperty` callback shorthand_.pluck(_.dropWhile(users, 'active', false), 'user');// => ['pebbles']// using the `_.property` callback shorthand_.pluck(_.dropWhile(users, 'active'), 'user');// => ['barney', 'fred', 'pebbles']
_.dropRightWhile(array, [predicate=_.identity], [thisArg]) :类似上
_.dropRightWhile(array, [predicate=_.identity], [thisArg])
_.takeWhile(array, [predicate=_.identity], [thisArg]) : 类似,第一个满足predicate 条件的元素开始截取数组
_.takeWhile(array, [predicate=_.identity], [thisArg])
_.takeRightWhile(array, [predicate=_.identity], [thisArg]) : 类似上
_.takeRightWhile(array, [predicate=_.identity], [thisArg])
_.fill(array, value, [start=0], [end=array.length]) : 替换 ,使用 value 值来填充(也就是替换) array,从start位置开始, 到end位置结束(但不包含end位置) (脏)
_.fill(array, value, [start=0], [end=array.length])
var array = [1, 2, 3];_.fill(array, 'a');console.log(array);// => ['a', 'a', 'a']_.fill(Array(3), 2);// => [2, 2, 2]_.fill([4, 6, 8], '*', 1, 2);// => [4, '*', 8]
_.findIndex(array, [predicate=_.identity], [thisArg]) : 替换 ,该方法类似 .find,区别是该方法返回的是符合 predicate条件的第一个元素的索引,而不是返回元素本身. [predicate= .identity]可以为(Function|Object|string),未找到则返回 -1. (干净)
_.findIndex(array, [predicate=_.identity], [thisArg])
var users = [ { 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': true }];_.findIndex(users, function(chr) { return chr.user == 'barney';});// => 0// using the `_.matches` callback shorthand_.findIndex(users, { 'user': 'fred', 'active': false });// => 1// using the `_.matchesProperty` callback shorthand_.findIndex(users, 'active', false);// => 0// using the `_.property` callback shorthand_.findIndex(users, 'active');// => 2
_.findLastIndex(array, [predicate=_.identity], [thisArg]) :类似上
_.findLastIndex(array, [predicate=_.identity], [thisArg])
_.flatten(array, [isDeep]) : 降维 将低数组维数,默认降低一维,如果isDeep = true则递归降到一维。 (干净)
_.flatten(array, [isDeep])
123456
_.flatten([1, [2, 3, [4]]]);// => [1, 2, 3, [4]]// using `isDeep`_.flatten([1, [2, 3, [4]]], true);// => [1, 2, 3, 4]
_.flattenDeep(array) : 相当于_.flatten(array, true)
_.flattenDeep(array)
_.indexOf(array, value, [fromIndex=0]) : 查找 获取value在数组 array中匹配的第一个全等元素的索引值 如果 fromIndex 值是负数, 则从array末尾起算. 如果 fromIndex为true时,对已排序的数组array执行二分查找。 (干净)
_.indexOf(array, value, [fromIndex=0])
_.indexOf([1, 2, 1, 2], 2);// => 1// using `fromIndex`_.indexOf([1, 2, 1, 2], 2, 2);// => 3// performing a binary search_.indexOf([1, 1, 2, 2], 2, true);// => 2
_.lastIndexOf(array, value, [fromIndex=array.length-1]) : 类似上
_.lastIndexOf(array, value, [fromIndex=array.length-1])
_.intersection([arrays]) : 过滤,查找 提取全等===,返回共有元素的数组。 (干净)
_.intersection([arrays])
_.intersection([1, 2], [4, 2], [2, 1]);// => [2]
_.union([arrays]) : 合并,去重 合并去重 (干净)
_.union([arrays])
_.union([1, 2], [4, 2], [2, 1]);// => [1, 2, 4]
_.xor([arrays]) : 合并,去重 合并去重,注意和union的区别 (干净)
_.xor([arrays])
_.xor([1, 2], [4, 2]);// => [1, 4]
_.uniq(array, [isSorted], [iteratee], [thisArg]) : 过滤,去重 isSorted (boolean): 是否排序好,对数组中的每个元素经过iteratee处理后去重 (干净)
_.uniq(array, [isSorted], [iteratee], [thisArg])
12345678910111213141516
_.uniq([2, 1, 2]);// => [2, 1]// using `isSorted` _.uniq([1, 1, 2], true);// => [1, 2]// using an iteratee function_.uniq([1, 2.5, 1.5, 2], function(n) { return this.floor(n);}, Math);// => [1, 2.5]// using the `_.property` callback shorthand_.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');// => [{ 'x': 1 }, { 'x': 2 }]
_.sortedIndex(array, value, [iteratee=_.identity], [thisArg]) : 插入 value (*): 插入的判断参数 [iteratee=_.identity]可以为(Function|Object|string): 遍历方法 [thisArg]: iteratee的绑定值,返回value 应该插入数组array位置的索引值 (干净)
_.sortedIndex(array, value, [iteratee=_.identity], [thisArg])
1234567891011121314151617
_.sortedIndex([40,20,10,50], 40);// => 3_.sortedIndex([4, 4, 5, 5], 5);// => 2var dict = { 'data': { 'thirty': 30, 'forty': 40, 'fifty': 50 } };// using an iteratee function_.sortedIndex(['thirty', 'fifty'], 'forty', function(word) { return this.data[word];}, dict);// => 1// using the `_.property` callback shorthand_.sortedIndex([{ 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x');// => 1
_.sortedLastIndex(array, value, [iteratee=_.identity], [thisArg]) : 类似上
_.sortedLastIndex(array, value, [iteratee=_.identity], [thisArg])
_.zip([arrays]) : 分组,合并 (多一维数组->一个二维数组)将各数组索引相同的组合成新二维数组,有这为undefined (干净)
_.zip([arrays])
_.zip(['fred', 'barney'], [30, 40, 50], [true, false]);// => [['fred', 30, true], ['barney', 40, false],[undefined, 50, undefined]]
_.unzip(array) : 分组,合并 (一个二维数组->一个二维数组)类似zip的逆过程,对zip处理过的数组进行再分组,将各数组索引相同的组合成新二维数组,有这为undefined (干净)
_.unzip(array)
var zipped = _.zip(['fred', 'barney'], [30, 40], [true, false]);// => [['fred', 30, true], ['barney', 40, false]]_.unzip(zipped);// => [['fred', 'barney'], [30, 40], [true, false]]
_.zipWith([arrays], [iteratee], [thisArg]) : 分组,合并 重新分组后对个数组进行统一处理 (干净)
_.zipWith([arrays], [iteratee], [thisArg])
_.zipWith([1, 2], [10, 20], [100, 200], _.add);// => [111, 222]
_.unzipWith(array, [iteratee], [thisArg]) : 分组,合并 对zip处理过的数组进行再分组,再对各数组进行统一处理 (干净)
_.unzipWith(array, [iteratee], [thisArg])
_.zipObject(props, [values=[]]) : 数组转对象 (干净)
_.zipObject(props, [values=[]])
_.zipObject([['fred', 30], ['barney', 40]]);// => { 'fred': 30, 'barney': 40 }_.zipObject(['fred', 'barney'], [30, 40]);// => { 'fred': 30, 'barney': 40 }
_.assign(object, [sources], [customizer], [thisArg]) : 合并,去重 相同属性时后面的对象可以会重写前面的对象 (干净)
_.assign(object, [sources], [customizer], [thisArg])
_.assign({ 'user': 'barney' }, { 'age': 40 }, { 'user': 'fred' });// => { 'user': 'fred', 'age': 40 }// using a customizer callbackvar defaults = _.partialRight(_.assign, function(value, other) { return _.isUndefined(value) ? other : value;});defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' });// => { 'user': 'barney', 'age': 36 }
_.create(prototype, [properties]) : 原型,继承 创建一个继承于prototype对象的新对象 (干净)
_.create(prototype, [properties])
_.create({aa:1}, {b:3});//{b: 3} b: 3 __proto__: aa: 1 __proto__: Object
_.defaults(object, [sources]) : 合并,去重 相同属性时,后面的对象会被忽略 (干净)
_.defaults(object, [sources])
_.defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' });// => { 'user': 'barney', 'age': 36 }
_.defaultsDeep(object, [sources]) : 合并,去重 相同属性时,后面的对象会被忽略,递归 (干净)
_.defaultsDeep(object, [sources])
_.defaultsDeep({ 'user': { 'name': 'barney' } }, { 'user': { 'name': 'fred', 'age': 36 } });// => { 'user': { 'name': 'barney', 'age': 36 } }
_.findKey(object, [predicate=_.identity], [thisArg]) : 查找 根据条件查找第一个符合的key (干净)
_.findKey(object, [predicate=_.identity], [thisArg])
var users = { 'barney': { 'age': 36, 'active': true }, 'fred': { 'age': 40, 'active': false }, 'pebbles': { 'age': 1, 'active': true }};_.findKey(users, function(chr) { return chr.age < 40;});// => 'barney' (iteration order is not guaranteed)// using the `_.matches` callback shorthand_.findKey(users, { 'age': 1, 'active': true });// => 'pebbles'// using the `_.matchesProperty` callback shorthand_.findKey(users, 'active', false);// => 'fred'// using the `_.property` callback shorthand_.findKey(users, 'active');// => 'barney'
_.findLastKey(object, [predicate=_.identity], [thisArg]) : 类似上,反向
_.findLastKey(object, [predicate=_.identity], [thisArg])
_.forIn(objectTar, [iteratee=_.identity], [thisArg]) : 遍历 传入 (value, key, object)参数中的object是objectTar,会遍历原型中的属性,返回对象objectTar (脏)
_.forIn(objectTar, [iteratee=_.identity], [thisArg])
function Foo() { this.a = 1; this.b = 2;}Foo.prototype.c = 3;_.forIn(new Foo, function(value, key) { console.log(key);});// => logs 'a', 'b', and 'c' let obj = new Foo;_.forIn(obj, function(value, key, obj) { obj[key] = value * 2});// {a:2,b:4,c:6}
_.forInRight(objectTar, [iteratee=_.identity], [thisArg]) : 类似上,反向
_.forInRight(objectTar, [iteratee=_.identity], [thisArg])
_.forOwn(objectTar, [iteratee=_.identity], [thisArg]) : 遍历 传入 (value, key, objectTar)参数中的object是objectTar,不会遍历原型中的属性 (脏)
_.forOwn(objectTar, [iteratee=_.identity], [thisArg])
function Foo() { this.a = 1; this.b = 2;}Foo.prototype.c = 3;_.forOwn(new Foo, function(value, key) { console.log(key);});// => logs 'a' and 'b' // {a:1,b:2,c:3}
_.forOwnRight(objectTar, [iteratee=_.identity], [thisArg]) : 类似上,反向
_.forOwnRight(objectTar, [iteratee=_.identity], [thisArg])
_.functions(object) : 查找,键名 查找对象中属性值为函数的属性名,返回属性名数组 (脏)
_.functions(object)
_.functions(_);// => ['after', 'ary', 'assign', ...]
_.get(object, path, [defaultValue]) : 查找 defaultValue 为找不到匹配值时返回
_.get(object, path, [defaultValue])
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'
_.result(object, path, [defaultValue]) : 查找 defaultValue 为找不到匹配值时返回,与_.get的区别在于,如果匹配到的是一个函数,则会执行函数返回值作为最后的结构,this指向父级object。
_.result(object, path, [defaultValue])
var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };_.result(object, 'a[0].b.c1');// => 3_.result(object, 'a[0].b.c2');// => 4_.result(object, 'a.b.c', 'default');// => 'default'_.result(object, 'a.b.c', _.constant('default'));// => 'default'
_.set(object, path, value) : 赋值,设置 返回object,如果不存在路径,则创建 (脏)
_.set(object, path, value)
var object = { 'a': [{ 'b': { 'c': 3 } }] };_.set(object, 'a[0].b.c', 4);console.log(object.a[0].b.c);// => 4_.set(object, 'x[0].y.z', 5);console.log(object.x[0].y.z);// => 5
_.has(object, path) : 类似上,判断是否存在该路径属性 查找,判断
_.has(object, path)
_.invert(object, [multiValue]) : 转换 交换key和value,如果存在value相同的属性,在multiValue不同是有不同的处理 (干净)
_.invert(object, [multiValue])
var object = { 'a': 1, 'b': 2, 'c': 1 };_.invert(object);// => { '1': 'c', '2': 'b' }// with `multiValue`_.invert(object, true);// => { '1': ['a', 'c'], '2': ['b'] }
_.keys(object) : 查找,键名 ,返回对象键名组成的数组,不包含原型上的
_.keys(object)
function Foo() { this.a = 1; this.b = 2;}Foo.prototype.c = 3;_.keys(new Foo);// => ['a', 'b'] (iteration order is not guaranteed)_.keys('hi');// => ['0', '1']
_.keysIn(object) :类似上,包含原型链上的 _.valuesIn(object) :类似下,包含原型链上的
_.keysIn(object)
_.valuesIn(object)
_.values(object) : 类似上
_.values(object)
function Foo() { this.a = 1; this.b = 2;}Foo.prototype.c = 3;_.values(new Foo);// => [1, 2] (iteration order is not guaranteed)_.values('hi');// => ['h', 'i']
_.mapKeys(object, [iteratee=_.identity], [thisArg]) : 替换,遍历,键名 遍历对象,根据iteratee替换键名 干净
_.mapKeys(object, [iteratee=_.identity], [thisArg])
_.mapKeys({ 'a': 1, 'b': 2 }, function(value, key) { return key + value;});// => { 'a1': 1, 'b2': 2 }
_.mapValues(object, [iteratee=_.identity], [thisArg]) : 替换,遍历 遍历对象,根据iteratee替换键值 干净
_.mapValues(object, [iteratee=_.identity], [thisArg])
_.mapValues({ 'a': 1, 'b': 2 }, function(n) { return n * 3;});// => { 'a': 3, 'b': 6 }var users = { 'fred': { 'user': 'fred', 'age': 40 }, 'pebbles': { 'user': 'pebbles', 'age': 1 }};// using the `_.property` callback shorthand_.mapValues(users, 'age');// => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
_.merge(object, [sources], [customizer], [thisArg]) : 合并,分组 根据customizer将sources合并到object中,并返回object (脏)
_.merge(object, [sources], [customizer], [thisArg])
12345678910111213141516171819202122232425262728
var users = { 'data': [{ 'user': 'barney' }, { 'user': 'fred' }]};var ages = { 'data': [{ 'age': 36 }, { 'age': 40 }]};_.merge(users, ages);// => { 'data': [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }] }// using a customizer callbackvar object = { 'fruits': ['apple'], 'vegetables': ['beet']};var other = { 'fruits': ['banana'], 'vegetables': ['carrot']};_.merge(object, other, function(a, b) { if (_.isArray(a)) { return a.concat(b); }});// => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot'] }
_.omit(object, [predicate], [thisArg]) 过滤,遍历,键名 创建一个新对象,包含object中不符合predicate的键值对 (干净)
_.omit(object, [predicate], [thisArg])
1234567
var object = { 'user': 'fred', 'age': 40 };_.omit(object, 'age');// => { 'user': 'fred' }_.omit(object, _.isNumber);// => { 'user': 'fred' }
_.pick(object, [predicate], [thisArg]) : 过滤,遍历,键名 创建一个新对象,包含object中符合predicate的键值对 (干净)
_.pick(object, [predicate], [thisArg])
var object = { 'user': 'fred', 'age': 40 };_.pick(object, 'user');// => { 'user': 'fred' }_.pick(object, _.isString);// => { 'user': 'fred' }
_.pairs(object) : 转换 将对象转为[[key1, value1], [key2, value2]] (干净)
_.pairs(object)
_.pairs({ 'barney': 36, 'fred': 40 });// => [['barney', 36], ['fred', 40]]
_.transform(object, [iteratee=_.identity], [accumulator], [thisArg]) : 遍历,转换 遍历object通过iteratee转成result (干净)
_.transform(object, [iteratee=_.identity], [accumulator], [thisArg])
_.transform([2, 3, 4], function(result, n) { result.push(n *= n); return n % 2 == 0;});// => [4, 9]_.transform({ 'a': 1, 'b': 2 }, function(result, n, key) { result[key] = n * 3;});// => { 'a': 3, 'b': 6 }