@@ -10,107 +10,112 @@ tags:
1010## 数组
1111
1212### 判断是否存在重复的元素
13+
1314``` js
14- const isRepeat = (arr ) => {
15- let hash = {};
16- for (let i in arr) {
17- if (hash[arr[i]]) {
18- return true ;
19- }
20- hash[arr[i]] = true ;
21- }
22- return false ;
15+ const isRepeat = (arr ) => {
16+ let hash = {};
17+ for (let i in arr) {
18+ if (hash[arr[i]]) {
19+ return true ;
2320 }
24-
21+ hash[arr[i]] = true ;
22+ }
23+ return false ;
24+ };
2525```
2626
2727e.g:
28+
2829``` js
29- const arr = [' hello' , ' world' , ' hello' , ' kitty' ];
30- isRepeat (arr) // true
30+ const arr = [' hello' , ' world' , ' hello' , ' kitty' ];
31+ isRepeat (arr); // true
3132```
3233
33- ### 按数组顺序查找缺失的第一项(从0开始 )
34+ ### 按数组顺序查找缺失的第一项(从 0 开始 )
3435
3536``` js
36- const findMissing = (arr , key = ' index' ) => {
37- let missing = null ;
38- for (let i = 0 ; i < arr .length ; i++ ) {
39- const result = arr .find (item => {
40- if (typeof item === ' object' ) {
41- return key in item && i === item[key];
42- } else {
43- return i === item;
44- }
45- });
46- if (! result) {
47- missing = i;
48- break ;
49- } else {
50- continue ;
51- }
52- }
53- return missing;
37+ const findMissing = (arr , key = ' index' ) => {
38+ let missing = null ;
39+ for (let i = 0 ; i < arr .length ; i++ ) {
40+ const result = arr .find ((item ) => {
41+ if (typeof item === ' object' ) {
42+ return key in item && i === item[key];
43+ } else {
44+ return i === item;
45+ }
46+ });
47+ if (! result) {
48+ missing = i;
49+ break ;
50+ } else {
51+ continue ;
5452 }
53+ }
54+ return missing;
55+ };
5556```
5657
5758e.g:
59+
5860``` js
59- const a = [0 ,2 , 4 , 1 , 5 ]
60- const m = this .findMissing (a); // 3
61+ const a = [0 , 2 , 4 , 1 , 5 ];
62+ const m = this .findMissing (a); // 3
6163
62- const a = [{i : 0 },{i : 3 },{i : 1 },{i : 6 }];
63- const m = this .findMissing (a, ' i' ); // 2
64+ const a = [{ i : 0 }, { i : 3 }, { i : 1 }, { i : 6 }];
65+ const m = this .findMissing (a, ' i' ); // 2
6466```
6567
6668### 移动数组元素到某个位置
6769
6870``` js
69- const moveItem = function (arr , fromIndex , toIndex ) {
70- const len = arr .length ;
71- if (toIndex > len - 1 ) {
72- toIndex = len - 1 ;
73- }
74- for (const k in arr) {
75- const item = arr[k];
76- if (k == fromIndex) {
77- arr .splice (k, 1 );
78- arr .splice (toIndex, 0 , item);
79- break ;
80- }
81- }
82- return arr;
83- };
71+ const moveItem = function (arr , fromIndex , toIndex ) {
72+ const len = arr .length ;
73+ if (toIndex > len - 1 ) {
74+ toIndex = len - 1 ;
75+ }
76+ for (const k in arr) {
77+ const item = arr[k];
78+ if (k == fromIndex) {
79+ arr .splice (k, 1 );
80+ arr .splice (toIndex, 0 , item);
81+ break ;
82+ }
83+ }
84+ return arr;
85+ };
8486```
87+
8588e.g:
89+
8690``` js
87- const arr = [1 ,2 , 3 , 4 , 5 ];
88- // 将下标2的元素移动到4位置
89- moveItem (arr, 2 , 4 ) // [1,2,4,5,3]
91+ const arr = [1 , 2 , 3 , 4 , 5 ];
92+ // 将下标2的元素移动到4位置
93+ moveItem (arr, 2 , 4 ); // [1,2,4,5,3]
9094```
9195
9296## 字符串
9397
9498### 测试字符长度,中文算两个字符
9599
96100``` js
97- const strlen = (str ) => {
98- let len = 0 ;
99- for (let i= 0 ; i< str .length ; i++ ) {
100- let c = str .charCodeAt (i);
101- if ((c >= 0x0001 && c <= 0x007e ) || (0xff60 <= c && c<= 0xff9f )) {
102- len++ ;
103- } else {
104- len+= 2 ;
105- }
106- }
107- return len;
101+ const strlen = (str ) => {
102+ let len = 0 ;
103+ for (let i = 0 ; i < str .length ; i++ ) {
104+ let c = str .charCodeAt (i);
105+ if ((c >= 0x0001 && c <= 0x007e ) || (0xff60 <= c && c <= 0xff9f )) {
106+ len++ ;
107+ } else {
108+ len += 2 ;
108109 }
110+ }
111+ return len;
112+ };
109113```
110114
111115e.g:
116+
112117``` js
113- strlen (' 你好123' ); // 7
118+ strlen (' 你好123' ); // 7
114119```
115120
116- 未完待续...
121+ 未完待续...
0 commit comments