|
| 1 | +/** |
| 2 | + * Lodash集合相关操作 |
| 3 | + * @private |
| 4 | + */ |
| 5 | + |
| 6 | +const _ = require('lodash') |
| 7 | + |
| 8 | +// countBy: 计数 |
| 9 | +console.log(_.countBy([6.1, 4.2, 6.3], Math.floor)) |
| 10 | +// => { '4': 1, '6': 2 } |
| 11 | +// The `_.property` iteratee shorthand. |
| 12 | +console.log(_.countBy(['one', 'two', 'three'], 'length')) |
| 13 | +// => { '3': 2, '5': 1 } |
| 14 | + |
| 15 | + |
| 16 | +// each: 遍历 |
| 17 | +// forEach: 遍历 |
| 18 | +_([1, 2]).forEach(function(value) { |
| 19 | + console.log(value) |
| 20 | +}) |
| 21 | +// => Logs `1` then `2`. |
| 22 | +_.forEach({ a: 1, b: 2 }, function(value, key) { |
| 23 | + console.log(key) |
| 24 | +}) |
| 25 | +// => Logs 'a' then 'b' (iteration order is not guaranteed). |
| 26 | + |
| 27 | + |
| 28 | +// every:断言,返回true|false |
| 29 | +_.every([true, 1, null, 'yes'], Boolean) |
| 30 | +// => false |
| 31 | +const users = [ |
| 32 | + { user: 'barney', age: 36, active: false }, |
| 33 | + { user: 'fred', age: 40, active: false } |
| 34 | +] |
| 35 | +// The `_.matches` iteratee shorthand. |
| 36 | +console.log(_.every(users, { user: 'barney', active: false })) |
| 37 | +// => false |
| 38 | +// The `_.matchesProperty` iteratee shorthand. |
| 39 | +console.log(_.every(users, ['active', false])) |
| 40 | +// => true |
| 41 | +// The `_.property` iteratee shorthand. |
| 42 | +console.log(_.every(users, 'active')) |
| 43 | +// => false |
| 44 | + |
| 45 | + |
| 46 | +// filter: 过滤 |
| 47 | +const userList = [ |
| 48 | + { user: 'barney', age: 36, active: true }, |
| 49 | + { user: 'fred', age: 40, active: false } |
| 50 | +] |
| 51 | +console.log(_.filter(userList, function(o) { return !o.active })) |
| 52 | +// => objects for ['fred'] |
| 53 | +// The `_.matches` iteratee shorthand. |
| 54 | +console.log(_.filter(userList, { age: 36, active: true })) |
| 55 | +// => objects for ['barney'] |
| 56 | +// The `_.matchesProperty` iteratee shorthand. |
| 57 | +console.log(_.filter(userList, ['active', false])) |
| 58 | +// => objects for ['fred'] |
| 59 | +// The `_.property` iteratee shorthand. |
| 60 | +console.log(_.filter(userList, 'active')) |
| 61 | +// => objects for ['barney'] |
| 62 | + |
| 63 | +// reject: filter反方法,过滤非真值 |
| 64 | + |
| 65 | + |
| 66 | +// find: 查询 |
| 67 | +// groupBy : 分组 |
| 68 | +// includes: 包含 |
| 69 | +// keyBy:迭代函数遍历,创建一个对象组成 |
| 70 | +// map: 迭代函数遍历,返回数组 |
| 71 | +// orderBy: 指定迭代函数进行排序 |
| 72 | +// partition:创建一个分成两组的元素数组 |
| 73 | +// reduce: 通过迭代函数遍历 |
| 74 | +// sample: 集合中获取随机值 |
| 75 | +// shuffle: 集合元素随机打乱 |
| 76 | +// size: 返回集合的长度,支持类数组、字符串、对象 |
| 77 | +// some: 筛选,判断是否存在,返回true|false |
| 78 | +// sortBy: 创建元素数组,迭代函数处理结果升序排序 |
0 commit comments