javascriptよくある方法のまとめの一つ、配列文字列関連
90607 ワード
(慕課網に転載)
原文のリンク:https://www.imooc.com/article/46933
githubアドレス:https://github.com/dorseysen/notes-about-javascript-methods/blob/master/about-string-and-array.html
原文のリンク:https://www.imooc.com/article/46933
githubアドレス:https://github.com/dorseysen/notes-about-javascript-methods/blob/master/about-string-and-array.html
1 DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>Titletitle>
6 head>
7 <body>
8 <script>
9 // :
10 // ,
11 /**
12 * 1、split : ( : )
13 * 2、join : ( : )
14 * 3、reserve : ( : )
15 * 4、slice : ( : )
16 * 5、splice : ( : )
17 * 6、sort : ( : )
18 * 7、concat : ( : , )
19 * 8、shift : ,
20 * 9、unshift : (shift unshift , ie6 )
21 * 10、push : , ( : )
22 * */
23 //1、split(reg); // , ,
24 let str0 = "my name is dorsey!";
25 console.log(str0.split(''));// ['m','y',' ','n','a','m','e',' ','i','s',' ','d','o','r','s','e','y','!'];
26 console.log(str0.split(' ')); //["my","name","is","dorsey!"]
27 //2、join(reg);// , , , , , join
28 let arr0 = ["my","name","is","dorsey!"];
29 console.log(arr0.join('-')); // “-” my-name-is-dorsey!
30 console.log(arr0.join(' ')); // “ ” my name is dorsey! ,
31 //3、reverse(); ,
32 let arr1 = ["my","name","is","dorsey!"];
33 console.log(arr1.reverse());["dorsey!","is","name","my"]; //
34 // 1-2-3
35 let str1 = " !";
36 console.log(str1.split('').reverse().join(''));
37 //4、slice(start,end); ,
38 let str2 = "my name is dorsey!";
39 console.log(str2.slice(3,7)); // 4~ 7 4,5,6,7
40 let arr2 = ["my","name","is","dorsey!"];
41 console.log(arr2.slice(1, 3)); // , 2~3
42 //5、splice(index,howmany,item1,item2...): , item , index ,index ,howmany
43 let str3 = ["my","name","is","dorsey!"];
44 str3.splice(2,2); // str3[2] , 2 , "is" "dorsey!"
45 console.log(str3);//["my","name"]
46 let arr3 = ["my","name","is","dorsey!"];
47 arr3.splice(2,0,2);// howmany , 0 ,
48 console.log(arr3);//["my","name",2,"is","dorsey!"];
49 //6、sort(): , ASCLL , , 415 6,415 4 6 ,4 , 415 6
50 let arr4 = [1,2,6,3,'b','baoh',457,712,41,62,"a","A",''];
51 // A 65,a 97, 0~9 ascll 48 57 , , C
52 arr4.sort();
53 console.log(arr4);//[1, 2, 3, 312, 41, 457, 6, 62, "A", "a"]
54 // Unicode , ascll , , , A a
55 // sort , , , , ? ,
56 let arr5 = [1,25,10,7,100,2560,13,-3281,-23,0];
57 function mun1(a,b){
58 return a-b;
59 }
60 console.log(arr5.sort(mun1));
61 //7、concat(): , , ,
62 let arr6 = ["my","name","is","dorsey!"];
63 let arr7 = ["I'm","from","China.",[" ","hello"]]; // concat , 。
64 console.log(arr6.concat(arr7)); //["my", "name", "is", "dorsey!", "I'm", "from", "China."];
65 console.log(arr6.concat('dahids')); //["my", "name", "is", "dorsey!", "dahids"]
66 //8、shift(): ,
67 let arr8 = ["my","name","is","dorsey!"];
68 console.log(arr8.shift()); //my
69 console.log(arr8); //["name", "is", "dorsey!"]
70 //9、unshift(): , , , ie6 firefox2.0
71 let arr9 = ["my","name","is","dorsey!"];
72 arr9.unshift(' !', 'hello');
73 console.log(arr9);
74 //10、push(): ,
75 let arr10 = ["my","name","is","dorsey!"];
76 arr10.push(' !', 'hello');
77 console.log(arr10);
78 //1、split ,join
79 // : , split String ,
80 // , ,
81 // , , , while() , ,
82 let Split = function(string,str){
83 let rule = new RegExp(str),
84 accept = [], //
85 str0 = string; // ,
86 // C/C++ for , slice,
87 while(1){
88 if(rule.test(str0) && str !== ''){
89 accept.push(str0.slice(0,str0.indexOf(str)));
90 str0 = str0.slice((str0.indexOf(str)+str.length),str0.length);
91 }
92 else {
93 if(str === '' ) {
94 for(let i = 0; i < str0.length; i ++ ){
95 accept.push(str0[i]);
96 }
97 break;
98 }
99 else{
100 accept.push(str0);break;
101 }
102 }
103 }
104 return accept;
105 }
106 console.log(Split(str0,''));
107 console.log(str0.split(''));
108 //3、 reverse , ,
109 let Reverse = function(arr){
110 let accept = [];
111 for(let i = 0;i < arr.length;i++){
112 accept[i] = arr[arr.length-1-i];
113 }
114 return accept;
115 }
116 console.log(Reverse(arr2));
117 console.log(arr2.reverse());
118 //4、slice : ,
119 let Slice = function(start,end,val){
120 let accept;
121 if(typeof(val) === "string"){
122 accept = '';
123 for(let i = start;i < (end > val.length ? val.length : end ); i++){
124 accept += val[i];
125 }
126 }
127 else {
128 let j = 0;
129 accept = [];
130 for(let i = start;i < (end > val.length ? val.length : end ); i++){
131 accept[j] = val[i];
132 j++;
133 }
134 }
135 return accept;
136 }
137 console.log(Slice(3, 100, str2));
138 console.log(str2.slice(3, 100));
139 console.log(Slice(2, 8, arr2))
140 console.log(arr2.slice(2, 8));
141 //6、sort : , , , ,
142 // , ,
143 // ! ! !
144 let Sort = function(arr,order){ //order StoL LtoS,StoL ( - ),LtoS ( - )
145 let accept = arr,mid;
146 if(accept.length > 1){
147 for(let i = 1; i < accept.length; i ++ ){
148 for(let j = 0; j < i; j ++ ){
149 if((accept[i] < accept[j] && order === 'StoL') || (accept[i] > accept[j] && order === 'LtoS')){ //
150 mid = accept[i]; // number , ( mid)
151 accept[i] = accept[j];
152 accept[j] = mid; // mid
153 }
154 }
155 }
156 }
157 return accept;
158 }
159 // // sort
160 let sortTest = [1,25,10,7,100,2560,13,-3281,-23,0];
161 console.log(Sort(sortTest,'LtoS'));
162 //7、concat : ,
163 let Concat = function(){
164 let accept = [];
165 for(let i = 0; i < arguments.length; i ++){
166 if(typeof (arguments[i]) !== "string"){
167 for(let j = 0; j < arguments[i].length; j++){
168 accept.push(arguments[i][j]);
169 }
170 }
171 else {
172 accept.push(arguments[i]);
173 }
174 }
175 return accept;
176 }
177 console.log(Concat(arr6, 'dasoai','dsaoidha',' ')); //
178 console.log(arr6.concat('dasoai','dsaoidha',' ')); //["my", "name", "is", "dorsey!", "dasoai", "dsaoidha", " "]
179 //8、shift :
180 let Shift = function(arr){
181 let firstVal = arr[0];
182 for(let i = 0; i < arr.length-1; i++){
183 arr[i] = arr[i + 1];
184 }
185 arr.length -=1;
186 return firstVal;
187 }
188 let shiftTest = [1,25,10,7,100,2560,13,-3281,-23,0];
189 console.log(Shift(shiftTest)); //
190 console.log(shiftTest); //
191 //9、unshift :
192 let Unshift = function(arr){
193 arr.length += arguments.length-1;
194 for(let i = arr.length;i > arguments.length-1; i --){
195 arr[i-1] = arr[i-arguments.length];
196 }
197 for(let i = 0 ; i < arguments.length-1 ; i ++){
198 arr[i] = arguments[i+1];
199 }
200 return arr.length;
201 }
202 let unshiftTest = [1,25,10,7,100,2560,13,-3281,-23,0];
203 console.log(Unshift(unshiftTest,"hello","world")); // 12
204 console.log(unshiftTest); //
205 //10、push : , unshift
206 let Push = function(arr){
207 arr.length += arguments.length - 1;
208 for(let i = 1; i < arguments.length ; i ++){
209 arr[arr.length - arguments.length + i] = arguments[i];
210 }
211 return arr.length;
212 }
213 let pushTest = [1,25,10,7,100,2560,13,-3281,-23,0];
214 console.log(Push(pushTest,"hello","world")); // 12
215 console.log(pushTest); //
216 script>
217 body>
218 html>