风华正茂、时光流逝、真爱时光、努力创建辉煌。

某面试题

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>

</html>
<script>

    /* 
        cookie字符串转对象
        输入‘foo=bar;equation=E%3Dmc%5E2’
    */
    const parseCookie = (str = '') => {
        return str.split(';')?.reduce((a, b) => {
            let key = /\w+(?=\=)/.exec(b)
            let val = /(?<=\=)[\w%]+/.exec(b)[0]
            let obj = {
                [key]: unescape(val)
            }
            return { ...a, ...obj }
        }, {})
    }
    // console.log(parseCookie('foo=bar;equation=E%3Dmc%5E2'))
    /* 
        找出对象中符合的项
        输入{a:1,b:'2',c:3} 条件item=>typeof x ==='string'
        输入{b:'2'}
    */
    const pickBy = (obj, fn) => {
        let _obj = {};
        for (let i in obj) {
            fn(obj[i]) && (_obj[i] = obj[i])
        }
        return _obj
    }
    pickBy({ a: 1, b: '2', c: '3' }, item => typeof item === 'string')
    /* 
        手机号加密
        输入 15806516662,“*”,3,4  从第三位开始的后四位替换成*
        输出158****6662
    */
    const secretMobile = (phone = '', bol = "*", strNum = 3, size = 4) => {
        let _phone = phone + ''
        return _phone.replace(new RegExp(`(?<=.{${strNum}})[0-9]{${size}}`), (match) => {
             let i=''
            while (size--) {
                i += bol
            }
            return i
        })
       
    }
    let a = secretMobile(15806516662, '*', 3,4)
    console.log(a)
</script>

  

posted @ 2021-03-12 00:35  野马,程序源改造新Bug  阅读(35)  评论(0编辑  收藏  举报