es6基础(6)--数组扩展

 1 //数组扩展
 2 {
 3     let arr=Array.of(3,4,6,7,9,11);//可以是空
 4     console.log('arr=',arr);//[3,4,6,7,9,11]
 5 }
 6 {
 7     //Array.from把伪数组或者集合变为数组
 8     let p=document.querySelectorAll('p');
 9     let pArr=Array.from(p);
10     pArr.forEach(function(item){
11         console.log(item.textContent);
12     })
13     //类似map
14     console.log(Array.from([1,3,5],function(item){
15         return item+2;
16     }))//[3,5,7]
17 }
18 {
19     //fill把数组中所有的都变成一个值
20     console.log('fill-7',[1,'a',undefined].fill(7));
21     //替换从第一个1开始到第三个3(1,3],被7替换
22     console.log('fill,pos',['a','b','c'].fill(7,1,3));
23 }
24 {
25     //输出索引
26     for(let index of ['1','c','ks'].keys()){
27         console.log('keys',index);
28     }
29     //输出值
30     for(let value of ['1','c','ks'].values()){
31         console.log('values',value);
32     }
33     //输出索引和值
34     for(let [index,value] of ['1','c','ks'].entries()){
35         console.log('entries',index,value);
36     }
37 }
38 {
39     //copyWithin(从哪个位置开始替换,从哪个位置开始读取,从哪个位置截止)
40     console.log([1,2,3,4,5].copyWithin(0,3,4));//[4,2,3,4,5]
41 }
42 {
43     //find查找,只会找满足条件的第一个值
44     console.log([1,2,3,4,5,6].find(function(item){
45         return item>3;
46     }))
47     //find查找,只会找满足条件的第一个值的下标
48     console.log([1,2,3,4,5,6].findIndex(function(item){
49         return item>3;
50     }))
51 }
52 {
53     //查看数组是否存在(x),可以找到NaN
54     console.log([1,2,NaN].includes(1))//true
55     console.log([1,2,NaN].includes(NaN))//true
56 }

猜你喜欢

转载自www.cnblogs.com/chenlw/p/9209582.html