1. Flat nested array /flat implementation

Description: Expands and tils a nested array into a single layer array.

let array = [1[1.2.3], [1[2, {}]] ]
handle(array) // [1, 1, 2, 3, 1, 2, {}]
Copy the code

Method one:

const handle = array= > JSON.parse(` [The ${JSON.stringify(array).replace(/\[|]/g.' ')}] `)
handle(array)   // [1, 1, 2, 3, 1, 2, {}]
Copy the code

Parse ()/ json.stringify (), string.prototype.replace ()

Method 2:

const handle = array= > array.reduce((accumulator, currentValue) = > accumulator.concat(Array.isArray(currentValue) ? handle(currentValue): currentValue), [])
handle(array)   // [1, 1, 2, 3, 1, 2, {}]
Copy the code

Array.prototype.reduce(), array.prototype.concat ()

Method 3:

const handle = array= > {
    while(array.some(item= > Array.isArray(item))) { array = [].concat(... array) }return array
}
handle(array)   // [1, 1, 2, 3, 1, 2, {}]
Copy the code

While, array.prototype.some (

Other methods:……

2. Array deduplication

Description: Filter out duplicate elements in an array.

let array = [1.2.1.'3'.'3'.0 , 1]
handle(array)   // [1, 2, '3', 0]
Copy the code

Method one:

const handle = array= > [...new Set(array)]
handle(array)   // [1, 2, '3', 0]
Copy the code

Set

Method 2:

const handle = array= > array.reduce((accumulator, currentValue) = > {
    !accumulator.includes(currentValue) && accumulator.push(currentValue)
    return accumulator
}, [])
handle(array)   // [1, 2, '3', 0]
Copy the code

Array.prototype.includes()

Method 3:

const handle = array= > {
    let map = new Map(a)return array.filter(item= > map.has(item) ? false : map.set(item))
}
handle(array)   // [1, 2, '3', 0]
Copy the code

Map, array.prototype.filter ()

Other methods:……

3. Simulate Call implementation

    Function.prototype.Call = function(){
        let args = Array.from(arguments), context = args.shift()
        context = Object(context)
        context.fn = this
        letresult = context.fn(... args)return (delete context.fn) && result
    };
Copy the code

4. Simulate the BIND implementation

Function.prototype.bind = function () {
    let self = this, args = Array.from(arguments), context = args.shift();
    return function () {
        returnself.apply(context, args.concat(... arguments)) } }Copy the code

I need to apply a call to my computer

5. Simulate the New implementation

const handle = function() {
    let fn = Array.prototype.shift.call(arguments)
    let obj = Object.create(fn.prototype)
    let o = fn.apply(obj, arguments)
    return typeof o === 'object' ? o : obj
}
Copy the code

Object.create()

6. Format numbers

const num = 123456789;
const handle = num= > String(num).replace(/\B(? =(\d{3})+(? ! \d))/g.', ')
handle(num) / / 123456789
Copy the code

Regular expressions, String.prototype.replace()

7. Palindrome judgment

const num = 123456654321;
const str = 'abababababab';
const handle = params= > {
    let str_1 = String(params).replace(/[^0-9A-Za-z]/g.' ').toLowerCase()
    let str_2 = str_1.split(' ').reverse().join()
    return str_1 === str_2 ? true : false
}
handle(num) // true
handle(str) // false
Copy the code

String.prototype.split(), array.prototype.join ()

8. Function throttling

The timer

const handle = (fn, interval) = > {
    let timeId = null;
    return function() {
        if(! timeId) { timeId = setTimeout((a)= > {
                fn.apply(this.arguments)
                timeId = null
            }, interval)
        }
    }
}
Copy the code

Window.settimeout

The time stamp

const handle = (fn, interval) = > {
    let lastTime = 0
    return function () {
        let now = Date.now();
        if (now - lastTime > interval) {
            fn.apply(this.arguments)
            lastTime = now
        }
    }
}
Copy the code

9. Function stabilization

const handle = (fn, delay) = > {
    let timeId
    return function() {
        if (timeId) clearTimeout(timeId)
        timeId = setTimeout((a)= > {
            fn.apply(this.arguments)
        }, delay)
    }
}
Copy the code

The difference between function throttling and function anti-shake: Function throttling and function anti-shake are easy to confuse, so to speak. For function throttling, there are frequent knocks outside the door, but the doorman decides whether to open the door at a fixed time. For function anti-shake, someone knocks at the door frequently, and the guard presses the last knock to decide whether to open the door.

Window. ClearTimeout

10. Deep copy

    const handle = function deepClone(params) {
        if (Array.isArray(params)) {
            return params.reduce((accumulator, currentValue) = >{(typeof currentValue === 'object')? accumulator.push(deepClone(currentValue)) : accumulator.push(currentValue)return accumulator
            }, [])
        } else {
            return Reflect.ownKeys(params).reduce((accumulator, currentValue) = >{(typeof params[currentValue] === 'object')? accumulator[currentValue] = deepClone(params[currentValue]) : accumulator[currentValue] = params[currentValue]return accumulator
            }, {})
        }
    }
Copy the code

11. Publish and subscribe

class Pubsub {
    constructor() {
        this.handles = {}
    }
    subscribe(type, handle) {
        if (!this.handles[type]) {
            this.handles[type] = []
        }
        this.handles[type].push(handle)
    }
    unsubscribe(type, handle) {
        let pos = this.handles[type].indexOf(handle)
        if(! handle) {this.handles.length = 0
        } else {
            ~pos && this.handles[type].splice(pos, 1)
        }
    }
    publish() {
        let type = Array.prototype.shift.call(arguments)
        this.handles[type].forEach(handle= > {
            handle.apply(this.arguments)}}}const pub = new Pubsub()
pub.subscribe('a'.function() {console.log('a'. arguments)}) pub.publish('a'.1.2.3)
// a 1 2 3
Copy the code