get和set就不说了,对付Typescript报错的常客。
[{ user: 'barney', age: 36, active: true }, { user: 'fred', age: 40, active: false }, { user: 'travis', age: 37, active: true}, { user: 'barney', age: 35, active: true }]
=>
[{ "user": "barney", "age": 35, "active": true }, { "user": "barney", "age": 36, "active": true }, { "user": "fred", "age": 40, "active": false }, { "user": "travis", "age": 37, "active": true }]
result = _.sortBy(users, ['user', 'age']); // 按多个字段排序
[{name: "GENEROUS", tag: "大量", value: 0},{name: "ACCURATE", tag: "精确", value: 1},{name: "CUSTOM", tag: "自定义", value: 2}]
=>
[{name: "GENEROUS", tag: "大量", value: 0},{name: "ACCURATE", tag: "精确", value: 1}]
result = _.filter(enums, (e) => e.name !== 'CUSTOM'); // 删除特殊值
[{user:'barney',age:36,active:true},{user:'fred',age:40,active:false},{user:'barney',age:37,active:true}]
=>
[{"user":"barney","age":36,"active":true},{"user":"fred","age":40,"active":false}]
result = _.uniqBy(users,'user'); // 根据user去重
[{ 'user': 'barney', 'age': 36, 'active': true },{ 'user': 'fred', 'age': 40, 'active': false },{ 'user': 'pebbles', 'age': 1, 'active': true }]
=>
{ 'user': 'barney', 'age': 36, 'active': true }
result = _.find(users, 'active');
result = _.find(users, {'user': 'barney'}); // 找第一个符合条件的对象
[{ 'user': 'barney', 'age': 36, 'active': true },{ 'user': 'fred', 'age': 40, 'active': false },{ 'user': 'pebbles', 'age': 1, 'active': true }]
=>
true
_.some(users, {'user': 'barney'}); // 判定是否存在符合条件的对象
[{ id: 1, name: "n1" }, { id: 2, name: "n2" }, { id: 3, name: "n3" }, { id: 4, name: "n4" }]
=>
[1, 2, 3, 4]
result = _.map(list, 'id'); // 获取对象数组id的集合
{ pic1: 'a.png', pic2: 'b.png', pic3: 'c.png' }
=>
["a.png", "b.png", "c.png"]
result = _.values(pics); // 对象转换为数组
[0, '0', 1, false, 2, '', undefined, 3]
=>
['0', 1, 2, 3]
result = _.compact(list); // 过滤空值、0、false
{ GE: '个', SHI: '十', BAI: '百' }
=>
{ '个': 个, '十': 十, '百': 百 }
result = _.mapKeys(object, value => value); // 将对象的参数名替换为值的名称
{ 'a': 1, 'b': '2', 'c': 3 }
=>
{ 'a': 1, 'c': 3 }
result = _.pick(object, ['a', 'c']); // 选择需要的参数
{ 'a': 1, 'b': '2', 'c': 3 }
=>
{ 'a': 1, 'c': 3 }
result = _.omit(object, ['b']); // 删除不需要的参数
[1, 2]
[2, 3]
=>
[1, 3]
result = _.xor(list1, list2); // 找出两个数组不相交的部分
_.random(-100, 100) // 产生-100到100的随机数,第三个参数可选择浮点
_.join(['a', 'b', 'c'], '~'); => 'a~b~c' // 连接数组
_.debounce(() => {}, 500) // 防抖调用