1.将伪数组转换成数组
1.1Array.from
伪数组:arguments
ES5:
let arr = [].slice.call(arguments)
ES6:
let arr = Array.from(arguments)
from
可以接受第二个参数,来对每一个元素进行处理
let liContent = Array.from(li, ele => ele.textContent)
1.2扩展运算符
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
let li = document.querySelectorAll('li')
li是伪数组
console.log([...li])
1.3of()
将一组值转换成数组
Array.of(3,11,45,78,85)
1.4find()
, findIndex()
find
找到第一个符合条件的成员
let num = [1,2,3,4,5,6,7,-8,-9,-4].find(n => n<0)
findIndex
找到第一个符合条件的成员索引
1.5keys, values, entries
keys()
:对键名遍历
values()
:对值遍历
entries()
:遍历键值对
for(let index of ['a', 'b'].keys()){
console.log(index)
}
for(let ele of ['a', 'b'].values()){
console.log(ele)
}
for(let [index, ele] of ['a', 'b'].entries()){
}
1.6next
let ent = ['11', 12, 13, 14, 18, 89]
let it = ent.entries()
console.log(it.next().value)
console.log(it.next().value)
console.log(it.next().value)
console.log(it.next().value)
console.log(it.next().value)
1.7includes()
返回一个布尔值, 表示某个数组是否包含给定的值
console.log([11,12,13,14,15,65].includes(2))
->false
解决了之前ES5
中的indexOf
写法
console.log([1,2,3,4,8].indexOf('2')=== -1)
-> -1