lodash比较常用的方法

深拷贝:

let person = {
    name:"123",
    age:"18",
   store:[1,2,3,4], 
   function(x,y){
      return x + y
   }        
}
let depObj= _.cloneDeep(person)

数组去重:

let arr = [2, 1, 2, '2',true]
let newArr = _.uniq(arr)

console.log(newArr) // [2, 1, '2', true]

生成随机数:

let random1 = _.random(1, 10)
let random2 = _.random(5)

console.log(random1) // 2
console.log(random2) // 4

防抖(debounce):

const debounced = debounce(() => {
    console.log('等我一秒!');
}, 1000);

window.addEventListener('resize', debounced);

节流(throttle):

const throttled = throttle(() => {
    console.log('100ms最多触发一次');
}, 100);

window.addEventListener('resize', throttled);

_.pick:

创建一个从 object 中选中的属性的对象。

       let obj = {
                name:undefined,
                age:undefined,
            }
            let data = {
                name:"小明",
                age:"18",
                sex:"男"
            }
            obj = _.pick(data,Object.keys(obj))
            console.log(obj)//{name:"小明",age:"18"}

posted on 2022-05-26 15:14  久居我梦  阅读(141)  评论(0)    收藏  举报

导航