行业知识
lodash常用方法汇总
数组
(1)flattenDeep:将array递归为一���数组。
()_.flattenDeep([1, [2, [3, [4]], 5]]);// => [1, 2, 3, 4, 5]
(2)findIndex:该方法返回第一个通过 predicate 判断为真值的元素的索引值(index),而不是元素本身。
var users = [ { 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': true }];_.findIndex(users, function(o) { return o.user == 'barney'; });// => 0// The `_.matches` iteratee shorthand._.findIndex(users, { 'user': 'fred', 'active': false });// => 1// The `_.matchesProperty` iteratee shorthand._.findIndex(users, ['active', false]);// => 0// The `_.property` iteratee shorthand._.findIndex(users, 'active');// => 2 (3)indexOf:返回首次 value 在数组array中被找到的 索引值, 如果 fromIndex 为负值,将从数组array尾端索引进行匹配。
()_.indexOf([1, 2, 1, 2], 2);// => 1 // Search from the `fromIndex`._.indexOf([1, 2, 1, 2], 2, 2);// => 3
(4)pull:移除数组array中所有和给定值相等的元素,使用SameValueZero 进行全等比较。
var array = [1, 2, 3, 1, 2, 3]; _.pull(array, 2, 3);console.log(array);// => [1, 1]
(5)pullAllWith: 这个方法类似于_.pullAll,区别是这个方法接受 comparator 调用array中的元素和values比较。comparator 会传入两个参数:(arrVal, othVal)。
var array = [{ 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 }, { 'x': 5, 'y': 6 }]; _.pullAllWith(array, [{ 'x': 3, 'y': 4 }], _.isEqual);console.log(array);// => [{ 'x': 1, 'y': 2 }, { 'x': 5, 'y': 6 }] (6)pullAt:根据索引 indexes,移除array中对应的元素,并返回被移除元素的数组。
var array = [5, 10, 15, 20];var evens = _.pullAt(array, 1, 3); console.log(array);// => [5, 15] console.log(evens);// => [10, 20]
(7)remove:移除数组中predicate(断言)返回为真值的所有元素,并返回移除元素组成的数组。predicate(断言) 会传入3个参数: (value, index, array)。
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] (8)sortedUniq:这个方法类似_.uniq,除了它会优化排序数组。返回一个新的不重复的数组。
_.sortedUniq([1, 1, 2]);// => [1, 2]
(9)sortedUniqBy:这个方法类似_.uniqBy,除了它会优化排序数组。返回一个新的不重复的数组。
_.sortedUniqBy([1.1, 1.2, 2.3, 2.4], Math.floor);// => [1.1, 2.3]
(10)uniq:创建一个去重后的array数组副本。使用了SameValueZero 做等值比较。只有第一次出现的元素才会被保留。
_.uniq([2, 1, 2]);// => [2, 1]
(11)uniqBy:这个方法类似_.uniq ,除了它接受一个 iteratee (迭代函数),调用每一个数组(array)的每个元素以产生唯一性计算的标准。iteratee 调用时会传入一个参数:(value)。
_.uniqBy([2.1, 1.2, 2.3], Math.floor);// => [2.1, 1.2] // The `_.property` iteratee shorthand._.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');// => [{ 'x': 1 }, { 'x': 2 }] (12)uniqWith:这个方法类似_.uniq, 除了它接受一个 comparator 调用比较arrays数组的每一个元素。 comparator 调用时会传入2个参数: (arrVal, othVal)。
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }]; _.uniqWith(objects, _.isEqual);// => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }] (13)zip:创建一个分组元素的数组,数组的第一个元素包含所有给定数组的第一个元素,数组的第二个元素包含所有给定数组的第二个元素,以此类推。
_.zip(['fred', 'barney'], [30, 40], [true, false]);// => [['fred', 30, true], ['barney', 40, false]]
(14)unzip:这个方法类似于_.zip,除了它接收分组元素的数组,并且创建一个数组,分组元素到打包前的结构。(:返回数组的第一个元素包含所有的输入数组的第一元素,第一个元素包含了所有的输入数组的第二元素,依此类推。)
var zipped = _.zip(['fred', 'barney'], [30, 40], [true, false]);// => [['fred', 30, true], ['barney', 40, false]] _.unzip(zipped);// => [['fred', 'barney'], [30, 40], [true, false]]
(15)without:创建一个剔除所有给定值的新数组,剔除值的时候,使用SameValueZero做相等比较。
_.without([2, 1, 2, 3], 1, 2);// => [3]
(16)zipObject:这个方法类似_.fromPairs,除了它接受2个数组,第一个数组中的值作为属性标识符(属性名),第二个数组中的值作为相应的属性值。
_.zipObject(['a', 'b'], [1, 2]);// => { 'a': 1, 'b': 2 } (17)zipWith:这个方法类似于_.zip,不同之处在于它接受一个 iteratee(迭代函数),来 指定分组的值应该如何被组合。 该iteratee调用每个组的元素: (…group).
_.zipWith([1, 2], [10, 20], [100, 200], function(a, b, c) { return a + b + c;});// => [111, 222] (18)flatten:减少一级array嵌套深度。
_.flatten([1, [2, [3, [4]], 5]]);// => [1, 2, [3, [4]], 5]
集合
(1)groupBy:创建一个对象,key 是 iteratee 遍历 collection(集合) 中的每个元素返回的结果。 分组值的顺序是由他们出现在 collection(集合) 中的顺序确定的。每个键对应的值负责生成 key 的元素组成的数组。iteratee 调用 1 个参数: (value)。
_.groupBy([6.1, 4.2, 6.3], Math.floor);// => { '4': [4.2], '6': [6.1, 6.3] } // The `_.property` iteratee shorthand._.groupBy(['one', 'two', 'three'], 'length');// => { '3': ['one', 'two'], '5': ['three'] } (2)keyBy:创建一个对象组成, key(键) 是 collection(集合)中的每个元素经过 iteratee(迭代函数) 处理后返回的结果。 每个 key(键)对应的值是生成key(键)的最后一个元素。iteratee(迭代函数)调用1个参数:(value)。
var array = [ { 'dir': 'left', 'code': 97 }, { 'dir': 'right', 'code': 100 }]; _.keyBy(array, function(o) { return String.fromCharCode(o.code);});// => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }_.keyBy(array, 'dir');// => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } } (3)orderBy:此方法类似于_.sortBy,除了它允许指定 iteratee(迭代函数)结果如何排序。 如果没指定 orders(排序),所有值以升序排序。 否则,指定为"desc" 降序,或者指定为 “asc” 升序,排序对应值。
var users = [ { 'user': 'fred', 'age': 48 }, { 'user': 'barney', 'age': 34 }, { 'user': 'fred', 'age': 40 }, { 'user': 'barney', 'age': 36 }]; // 以 `user` 升序排序 再 `age` 以降序排序。_.orderBy(users, ['user', 'age'], ['asc', 'desc']);// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]] (4)partition:创建一个分成两组的元素数组,第一组包含predicate(断言函数)返回为 truthy(真值)的元素,第二组包含predicate(断言函数)返回为 falsey(假值)的元素。predicate 调用1个参数:(value)。
var users = [ { 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'fred', 'age': 40, 'active': true }, { 'user': 'pebbles', 'age': 1, 'active': false }]; _.partition(users, function(o) { return o.active; });// => objects for [['fred'], ['barney', 'pebbles']] // The `_.matches` iteratee shorthand._.partition(users, { 'age': 1, 'active': false });// => objects for [['pebbles'], ['barney', 'fred']] // The `_.matchesProperty` iteratee shorthand._.partition(users, ['active', false]);// => objects for [['barney', 'pebbles'], ['fred']] // The `_.property` iteratee shorthand._.partition(users, 'active');// => objects for [['fred'], ['barney', 'pebbles']] (5)sortBy:创建一个元素数组。 以 iteratee 处理的结果升序排序。 这个方法执行稳定排序,也就是说相同元素会保持原始排序。 iteratees 调用1个参数: (value)。
var users = [ { 'user': 'fred', 'age': 48 }, { 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }, { 'user': 'barney', 'age': 34 }]; _.sortBy(users, function(o) { return o.user; });// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]] _.sortBy(users, ['user', 'age']);// => objects for [['barney', 34], ['barney', 36], ['fred', 40], ['fred', 48]] _.sortBy(users, 'user', function(o) { return Math.floor(o.age / 10);});// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]] (6)map:创建一个数组, value(值) 是 iteratee(迭代函数)遍历 collection(集合)中的每个元素后返回的结果。 iteratee(迭代函数)调用3个参数:
function square(n) { return n * n;} _.map([4, 8], square);// => [16, 64] _.map({ 'a': 4, 'b': 8 }, square);// => [16, 64] (iteration order is not guaranteed) var users = [ { 'user': 'barney' }, { 'user': 'fred' }]; // The `_.property` iteratee shorthand._.map(users, 'user');// => ['barney', 'fred'] 语言
(1)cloneDeep:这个方法类似_.clone,除了它会递归拷贝 value。(注:也叫深拷贝)。
var objects = [{ 'a': 1 }, { 'b': 2 }]; var deep = _.cloneDeep(objects);console.log(deep[0] === objects[0]);// => false 对象
(1)at:创建一个数组,值来自 object 的paths路径相应的值。
var object = { 'a': [{ 'b': { 'c': 3 } }, 4] }; _.at(object, ['a[0].b.c', 'a[1]']);// => [3, 4] (2)get:根据 object对象的path路径获取值。 如果解析 value 是 undefined 会以 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' (3)has:检查 path 是否是object对象的直接属性。
var object = { 'a': { 'b': 2 } };var other = _.create({ 'a': _.create({ 'b': 2 }) }); _.has(object, 'a');// => true _.has(object, 'a.b');// => true _.has(object, ['a', 'b']);// => true _.has(other, 'a');// => false (4)invoke:调用object对象path上的方法。
var object = { 'a': [{ 'b': { 'c': [1, 2, 3, 4] } }] };_.invoke(object, 'a[0].b.c.slice', 1, 3);// => [2, 3] (5)pick:创建一个从 object 中选中的属性的对象。
var object = { 'a': 1, 'b': '2', 'c': 3 }; _.pick(object, ['a', 'c']);// => { 'a': 1, 'c': 3 } (6)merge:该方法类似_.assign, 除了它递归合并 sources 来源对象自身和继承的可枚举属性到 object 目标对象。如果目标值存在,被解析为undefined的sources 来源对象属性将被跳过。数组和普通对象会递归合并,其他对象和值会被直接分配覆盖。源对象从从左到右分配。后续的来源对象属性会覆盖之前分配的属性。
var object = { 'a': [{ 'b': 2 }, { 'd': 4 }]}; var other = { 'a': [{ 'c': 3 }, { 'e': 5 }]}; _.merge(object, other);// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] } 字符串
(1)endsWith:检查字符串string是否以给定的target字符串结尾。
_.endsWith('abc', 'c');// => true _.endsWith('abc', 'b');// => false _.endsWith('abc', 'b', 2);// => true (2)startsWith:检查字符串string是否以 target 开头。
_.startsWith('abc', 'a');// => true _.startsWith('abc', 'b');// => false _.startsWith('abc', 'b', 1);// => true (3)truncate:截断string字符串,如果字符串超出了限定的最大值。 被截断的字符串后面会以 omission 代替,omission 默认是 “…”。
_.truncate('hi-diddly-ho there, neighborino');// => 'hi-diddly-ho there, neighbo...' _.truncate('hi-diddly-ho there, neighborino', { 'length': 24, 'separator': ' '});// => 'hi-diddly-ho there,...' _.truncate('hi-diddly-ho there, neighborino', { 'length': 24, 'separator': /,? +/});// => 'hi-diddly-ho there...' _.truncate('hi-diddly-ho there, neighborino', { 'omission': ' [...]'});// => 'hi-diddly-ho there, neig [...]'
上一篇:高防云服务器售价