时间:2021-07-01 10:21:17 帮助过:4人阅读
一、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() 用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括ES6新增的数据结构Set和Map)
同时 Array.from还可以接受第二个参数,作用类似于数组的map方法,用来对每个元素进行处理,将处理后的值放入返回的数组。
{
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));
}四、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(p1,p2,p3) 从p1位置开始 用p2到p3-1的位置的数据进行覆盖
{
console.log([1,2,3,4,5].copyWithin(0,3,4));//[4,2,3,4,5]
}六、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}));
}{
console.log('number',[1,2,NaN].includes(1));//true
console.log('number',[1,2,NaN].includes(NaN));//true
}以上就是数组扩展新增的特性实例详解的详细内容,更多请关注Gxl网其它相关文章!