扁平化数组
function fn(arr) {
let newArr = []
arr.forEach((item) => {
if(Array.isArray(item)) {
newArr.push(...fn(item))
} else {
newArr.push(item)
}
})
return newArr
}
//或者
arr.join().split(',')
随机打乱数组
arr.sort(function fn() {return 0.5 - Math.random()})
promise.all()实现
function fn(promises) {
return new Promise(resolve, reject) {
promises = Array.from(promises)
let result = []
let count = 0
for(let item of promises) {
Promise.resolve(item).then(data => {
count++
result[i] = data
if(count === promises.length) {
resolve(result)
}
}, err => {
reject(err)
return
})
}
}
}