嗨!我是浮生code,一个努力向上爬的蜗牛程序员。关注我!!!带你每天向上爬🐌🐌🐌
1. flat
const array = [ 1, [ 2, [ 3, [ 4, [ 5 ] ] ] ] ]
// The default depth is 1
const flat1 = array.flat() // [ 1, 2, [ 3, [ 4, [ 5 ] ] ] ]
const flat2 = array.flat(2) // [ 1, 2, 3, [ 4, [ 5 ] ] ]
const flatAll = array.flat(Infinity) // [ 1, 2, 3, 4, 5 ]
2. Array.from
1).类数组对象
const arrayObj = {
0: '张三',
1: '李四',
length: 2
}
//good
const array1 = [].slice.call(arrayObj ) // ['张三', '李四']
//best
const array2 = Array.from(arrayObj ) // ['张三', '李四']
2).节点列表
const domsNodeList = document.querySelectorAll('div')
const domsArray = Array.from(domsNodeList) // [ dom, dom, dom, ... ]
3).Arguments
const logInfo = function () {
console.log('arguments', arguments)
console.log('Array.from arguments', Array.from(arguments))
}
logInfo('test', 100)
logInfo('test')
4).Array.from的第二个参数(类似 Array.map() )
onst array = [ 1, 2, 3 ]
const array2 = array.map((num) => num * 2) // [2, 4, 6]
const array3 = Array.from(array, (num) => num * 2) // [2, 4, 6]
3.Array.join()
const list = [1, 2, 3, 4, 5];
list.join(', '); // "1, 2, 3, 4, 5"