es6 - array for-chrome

 1 // 去重复
 2 Array.from(new Set([1, 1, 2, 3]));         // [1, 2, 3]
 3 console.log(Array.from(new Set([1, 1, 2, 3])));
 4 
 5 // 分解字符串
 6 Array.from('hello');                       // ["h", "e", "l", "l", "o"]
 7 console.log(Array.from('hello'));
 8 
 9 // 转换为ascill
10 Array.from('hello', x => x.charCodeAt(0)); // [104, 101, 108, 108, 111]
11 let ascill = Array.from('hello', x => x.charCodeAt(0));
12 console.log(ascill, typeof (ascill));
13 
14 
15 // 获取节点
16 let divs = document.querySelectorAll('div');
17 Array.from(divs).forEach(function (node) {
18     console.log(node);
19 });
20 
21 
22 // 数组合并
23 let i = [2, 3, 5, 6];
24 let j = [1, 2, 3, 4];
25 let k = Array.of('hello', 666, i, j);
26 console.log(k);
27 Array.of('hello');      // ["hello"]
28 Array.of(1, 2, 3);      // [1, 2, 3]
29 Array.of('blink', 182); // ["blink", 182]
30 
31 
32 // 替换数组内容
33 // target = 数组位置(3) ,目标等于start数组位置内(0 ~ array.length-1)
34 let copyWithin1 = [0, 1, 2, 3, 4].copyWithin(target = 3, start = 0);
35 console.log('copyWithin1:', copyWithin1);
36 
37 // target = 数组位置(3) ,目标等于start数组位置内(0 ~ array.length-1),结束位置是4
38 let copyWithin2 = [0, 1, 2, 3, 4].copyWithin(target = 0, start = 3, end = 4);  // [3, 1, 2, 3, 4]
39 console.log('copyWithin2:', copyWithin2);
40 
41 
42 
43 // fill  - 填充
44 [0, 1, 2, 3, 4].fill(5);                 // [5, 5, 5, 5, 5]
45 // 第二个以后开始fill填充
46 [0, 1, 2, 3, 4].fill(5, start = 2);        // [0, 1, 5, 5, 5]
47 // 第二个开始填充,下标4结束.
48 [0, 1, 2, 3, 4].fill(5, start = 2, end = 4); // [0, 1, 5, 5, 4]
49 
50 
51 
52 // 字符串包含 - es6
53 function startsWithLetterA(e, index, array) {
54     // 包含开头内容a  - es6 字符串搜索
55     console.log(e, index, array);
56     if (e.startsWith('a')) {
57         return e;
58     }
59 }
60 var cuteNames = ['jeff', 'marc', 'addy', 'francois'];
61 cuteNames.find(startsWithLetterA); // "addy"
62 
63 
64 
65 // 字符串包含 - 返回下标
66 function startsWithLetterA(element, index, array) {
67     if (element.startsWith('a')) {
68         return element;
69     }
70 }
71 var cuteNames = ['jeff', 'marc', 'addy', 'francois'];
72 
73 cuteNames.findIndex(startsWithLetterA); // 2

END...


posted @ 2018-07-31 13:02  Sunsin  阅读(265)  评论(0编辑  收藏  举报