眉目亦如画i

文章 评论 浏览 30

眉目亦如画i 2022-05-04 13:57:46
function flatAndSort (arr, map = {} ) {
    return arr.reduce((ret, curr) => {
        if (Array.isArray(curr)) {
            ret = ret.concat(flatAndSort(curr, map))
        } else {
            if (!map[curr]) {
                ret.push(curr)
                map[curr] = true
            }
        }
        return ret
    }, []).sort((a, b) => a - b)
}

第 11 题:将数组扁平化并去除其中重复数据,最终得到一个升序且不重复的数组

眉目亦如画i 2022-05-04 13:57:31

补充一点:类继承是单一继承结构,只有一个父类;而原型继承本质上是组合,它可以有多个父类,且不会产生层级分类这样的副作用。

第 7 题:ES5/ES6 的继承除了写法以外还有什么区别?

眉目亦如画i 2022-05-04 13:56:24
const reverseCase = str => {
    let s = '';
    for(let i of str){
        if(i === i.toUpperCase()){
            s += i.toLowerCase();
        }else {
           s += i.toUpperCase();
        }
    }
    return s;
};
reverseCase('abCDeFgHe'); // "ABcdEfGhE"

第 69 题: 如何把一个字符串的大小写取反(大写变小写小写变大写),例如 ’AbC' 变成 'aBc'

眉目亦如画i 2022-05-04 13:54:20
class MutiRequest {
    constructor(urls, max) {
        this.urls = urls
        this.max = max
        this.pending = []
        this.excuting = []
        this.result = []
        this.init(urls)
    }

    init(urls) {
        for (let i = 0; i < urls.length; i++) {
            this.add(fetchApi(urls[i]), i)
        }
    }

    add(promiseFn, index) {
        if (this.excuting.length === this.max) {
            return new Promise((resolve) => {
                this.pending.push(() => {
                    this.excute(promiseFn, resolve)
                })
            }).then((data) => {
                console.log(index, data)
                this.result[index] = data
                this.printOut()
            })
        } else {
            return new Promise((resolve) => {
                this.excute(promiseFn, resolve)
            }).then((data) => {
                console.log(index, data)
                this.result[index] = data
                this.printOut()
            })
        }
    }

    printOut () {
        if (this.result.length === this.urls.length) {
            console.log(this.result)
        }
    }

    excute(promiseFn, resolve) {
        this.excuting.push(promiseFn)
        promiseFn.then((data) => {
            resolve(data)
            this.excuting = this.excuting.filter((item) => {
                return item !== promiseFn
            })
            if (this.pending.length) {
                this.pending.shift()()
            }
        })
    }
}

// 模拟请求
const fetchApi = (url) => {
    return new Promise((resolve, reject) => {
        let random = Math.random() * 10
        setTimeout(() => {
            resolve('res:' + url)   
        }, 1000 * random)
    });
}

// 测试
let mutiTest = new MutiRequest([
    'https://api.xx.com/01',
    'https://api.xx.com/02',
    'https://api.xx.com/03',
    'https://api.xx.com/04',
    'https://api.xx.com/05',
    'https://api.xx.com/06',
    'https://api.xx.com/07',
    'https://api.xx.com/08',
    'https://api.xx.com/09',
    'https://api.xx.com/10',
    'https://api.xx.com/11',
    'https://api.xx.com/12',
    'https://api.xx.com/13',
    'https://api.xx.com/14',
    'https://api.xx.com/15',
], 2)

第 153 题:实现一个批量请求函数 multiRequest(urls, maxNum)

更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文