ES6学习总结是自己在学习过程中的总结,记笔记就是为了督促自己学习和复习,好记性不如烂笔头。如果有错误,感谢指出。
Array.of
变量转化成数组形式
{
let arr = Array.of(3,4,7,9,11);
console.log('arr=',arr);//[3,4,7,9,11]
let empty=Array.of();
console.log('empty',empty);//[]
}
Array.from
- 把为数组、集合转为真正的数组;
- 类似于map的用法;Array.from还可以接受第二个参数,用来对每个元素进行处理,将处理后的值放入返回的数组。
{
let p=document.querySelectorAll('p');
let pArr=Array.from(p);
pArr.forEach(function(item){
console.log(item.textContent);
});
console.log(Array.from([1,3,5],function(item){return item*2}));//[2,6,10]
}
fill(data,startIndex,endIndex-1)
填充数组,替换指定位置和长度的数组。如果只有一个data参数将全部进行替换,如果三个参数将从startIndex~endIndex-1全部替换为data。
{
console.log('fill-7',[1,'a',undefined].fill(7));//[7,7,7]
console.log('fill,pos',['a','b','c'].fill(7,1,3));
}
entries\keys\values
- keys(返回所有的数组下标)
- values(返回数组所有的value)
- entries(包括所有的key和values)
存在兼容性问题,浏览器不支持
{
for(let index of ['1','c','ks'].keys()){
console.log('keys',index);
}
for(let value of ['1','c','ks'].values()){
console.log('values',value);
}
for(let [index,value] of ['1','c','ks'].entries()){
console.log('values',index,value);
}
}
copyWithin
起始位置,从开始的长度,结束长度,不常用
{
console.log([1,2,3,4,5].copyWithin(0,3,4));//[4,2,3,4,5]
}
find/findIndex
- 查找元素是否存在
- find只找出第一个,就返回
- findIndex只找出第一个返回下标记
{
console.log([1,2,3,4,5,6].find(function(item){return item>3}));//4
console.log([1,2,3,4,5,6].findIndex(function(item){return item>3}));
}
includes
数组中包括某元,true false,find可以加函数,Nan不会报错
{
console.log('number',[1,2,NaN].includes(1));//true
console.log('number',[1,2,NaN].includes(NaN));//true
}
网友评论