配列と文字列に関する一般的な方法のまとめ


配列と文字列に関する一般的な方法のまとめ
  • 配列の一般的な方法
  • 配列エルゴード:
  • var arr = [1, true, "hello"];	
    document.write(arr);	
    document.write("
    "); //for var arr1 = ["red","blue","black","green"]; for(var i = 0; i < arr1.length; i++){ document.write(arr1[i]+"
    "); } //for in ( ) for(var i in arr){ document.write(arr1[i]+"
    "); }
  • 配列統合:
  • //  1.concat(  2)  -->                                 
    const arr = [12,34,56];
    const arr1 = ['wuhao','xiner'];
    const arr2 = arr.concat(arr1);
    console.log(arr2);//12,34,56,'wuhao','xiner'
    
  • 配列切り取り:
  • //  .slice(start,end)
    //-->              ,       ,     -->                (   end    )
    const arr = [1,4,6,7,5,8,5];
    const newArr = arr.slice(1,3);
    console.log(newArr);//[4,6]
  • 要素の削除、修正、追加:
  • //splice               ,  ,  
    -->  1:       ;  2:     ;  3:      	
    		
    //     :  .splice(1,1);                         
    const arr = [1,4,7,9,5,3,2];
    const newArr = arr.splice(1,1);
    console.log(newArr);//4	
    			
    //     :  .splice(1,1,  ...);      
    const newArr1 = arr.splice(1,1,55);
    console.log(newArr1);//[1,55,7,9,5,3,2]	
    			
    //     :  .splice(1,  ...);             
    const newArr2 = arr.splice(1,4);
    console.log(newArr2);//[1,4,4,7,9,5,3,2]
  • 要素の追加と削除:
  • //=============1,     (  ) -->    ==============     
    //   :   .push(  。。。。)-->         ,   ,    -->           
    const arr = [1,5,8,9,4,3];
    console.log(arr.push(2));//7
    					 
    //   :   .pop()-->         ,   ,    -->     
    console.log(arr.pop());//2
    								 	
    //==============2,       (  )	-->    ===========			   
    //   :  .push(  。。。。)-->         ,   ,    -->           
    console.log(arr.push(2));//7
    			   
    //   :  .shift()-->         ,   ,    -->     
    console.log(arr.shift());//1
    					   
    //   :  .unshift(  。。。。)-->         ,   ,    -->           
    console.log(arr.unshift(4));//7
  • 配列と文字列の間の変換:
  • //  .join(   ),                  
    //-->      -->           
    const arr = ['this','is','the','good','thing'];
    const str = arr.join(',');
    console.log(str);//'this,is,the,good,thing'
    
    //   .split(   ),               
    //-->      -->          
    const newArr = str.split(',');
    console.log(newArr);//['this','is','the','good','thing'];
    
     
  • ECMA 5配列方法を追加しました.
  • 検索要素の位置:
  • //  .indexof(      ,start       );
    //   :                         :          
    const arr = [2,3,5,7,9,4];
    const index = arr.indexof(4,1);
  • エルゴード配列:
  • //  .forEach( (item,index,array) => {});		  
    //    :item       index           array             
    const arr = [3,4,6,3,8,9];
    arr.forEach((item,index,arr)=>{
    //item --->     
    //index --->       
    //arr --->      
    document.write(item+"  "+index+"  "+arr+"
    "); })
  • エルゴード操作配列:
  • //  .map( (item,index,array) => {});
    //   :item       index           array      
    const  arr = [3,5,6,8,5,4];
    const newArr = arr.map( (item,index,array) => {
    return item + 2;//  ->  ->  
    });
  • 現在と前の和帰結:
  • //  .reduce( (pre,next,index,array) => {});          
    //   :pre      return       next           index         
    const arr = [2,3,5,6,8,6];
    const res = arr.reduce( (pre,next,index,array) => {
         return pre + next;
    });
  • フィルタリング:
  • //  ,return          ;
    //  .filter((item,index,arr) => {});
    const arr = [30,45,76,89,32,22];
    const res1 = arr.filter((item,index,arr) => {
    		return item > 30;	
    });		
  • someはreturnの後の条件が成立するかどうかを判断します.trueに戻ります.false
  • に戻ります.
    //some       return               true       false
    //  .some( (item,index,array) => {})
    const arr = [30,45,6,65,34,78];
    const res2 = arr.some( (item, index, arr) => {			
    return item == 30;//          30,           		
    });
  • 文字列の常用方法:
  • 文字列の大文字小文字:
  • //          
    const str = "WuhAo";
    console.log(str);//WuhAo
    
    //   .toUpperCase()--->   
    const str1 = str.toUpperCase();
    console.log(str1);//WUHAO
    
    //   .toLowerCase()--->   
    const str2 = str.toLowerCase();
    console.log(str2);//wuhao
  • 文字列の置換、分割:
  • //1,   .replace(     /     ,      ):-->         /     ,      -->//    :      ,       【 】                              (/abc/g)-->g   
    const str = "wuhaoHello";
    const str1 = str.replace(/^wuhao$/,"wenyan");
    console.log(str1);//wenyanHello 
    
    //===============================================================================		
    //2,   .substring(start,end);-->  :               -->    :                 【 】       
    const str2 = str.substring(0,3);
    console.log(str2);//wuh
    
    //===============================================================================			
    3,   .split(   ,        );-->  :   ,        -->    :          
    const str3 = "hello are you";
    const arr = str3.split(' ',1);
    console.log(arr);//hello,are 
  • 文字列の検索方法:
  • //1,   .indexof(  ,       )-->             -->    :                   ,             ,      0
    const str = "wuhao";
    const strIndex = str.indexof('u',2);
    console.log(strIndex);//0
    
    //2,   .lastIndexof(  )-->     -->    :               ,              ,      -1
    const strIndex1 = str.lastIndexof('0');
    console.log(strIndex1);//4
    	
    //3,   .search(     /  )-->                  "abc"  /abc/ ig    i      
    const flag = str.search('wu');
    console.log(flag);//true
  • 文字列その他の方法:
  • //1,   .sup(); str.sub();   document.write();   
    document.write("hello".sup()+"
    "); //2, .charAt( ); --> --> const str = "hello"; const str1 = str.charAt(1); console.log(str1);//e //3, .charCodeAt( ); --> --> ACSII //4,String.fromCharCode(ACSII ) --> ACSII --> //5, 1.concat( 2); --> --> ( + ) const str2 = "wuhao"; const str3 = "xiner"; const str4 = str2.concat(str3); console.log(str4);//wuhaoxiner