javascriptの配列Aray

5900 ワード

  • 配列の作成と修正
  • var a=new Array();
    console.log(a);
    a=new Array(3);
    console.log(a);//   3   
    a=new Array("abc","xyz");
    console.log(a);
    //  ,     
    a=Array(3);
    a=Array("abc","xyz");
    //      
    a=[];
    a=[3];
    a=["abc","xyz"];
    console.log(a.length);//     
    console.log(a[1]);//        
    a.length=1;//    
    console.log(a[1]);//undefined
    a[a.length]="asd";//        asd
    console.log(a);//  
    a[a.length]="zxc";//     
    console.log(a);//  
    a=["0","1","2"];
    a[99]=99;//   100  99
    console.log(a.length);//   100
    console.log(a[4]);//undefined,         
    
  • タイプ検出
  • a=["qwe","asd","zxc"];
    console.log(a instanceof Array);//       ,         BUG
    console.log(Array.isArray(a));//      ,         ,IE8           
    
  • 変換方法
  • console.log(a.toString());//qwe,asd,zxc
    console.log(a.valueOf());//["qwe","asd","zxc"]
    console.log(a);//["qwe","asd","zxc"]
    //alert(a.toString);
    //alert(a.valueOf());
    //alert(a);
    console.log(a.join("||"));//qwe||asd||zxc,      
    a.length=5;
    console.log(a.join("||"));//qwe||asd||zxc||||
    console.log(a.toString());//qwe,asd,zxc,,
    
  • Arayスタック動作
  • var a=[];
    var length=a.push("wer","sdf");//    
    console.log(length);//  
    console.log(a);//
    length=a.push("w");//   
    console.log(length);//  
    console.log(a);//
    var item=a.pop();//      
    console.log(item);//     
    console.log(a);//
    item=a.pop();//   
    console.log(item);//     
    console.log(a);//
    
  • Arayキュー動作
  • a=["z","x","c"];
    item=a.shift();//     
    console.log(item);//
    console.log(a);//
    length=a.unshift("as");//        
    console.log(length);//
    console.log(a);//
    
  • Arayの反転とソート
  • a=[1,2,3,4,5];
    console.log(a.reverse());//  ,   
    a=[1,2,3,4,10,5];
    console.log(a.reverse());//5,10,4,3,2,1  ,   
    console.log(a.sort());//1,10,2,3,4,5           ,sort             ,10    2 
    //sort         ,   java   
    function compare(a,b){
        if(ab)return -1;
        else return 0;
    }
    console.log(a.sort(compare));//10,5,4,3,2,1                
    
  • 配列の接続と切り取り
  • a=[1,2];
    console.log(a.concat(5,6,[3,4]));//[1,2,5,6,3,4]
    a=[1,2,3,4,5,6];
    console.log(a.slice(3));//[4,5,6]          
    console.log(a.slice(3,5));//[3,4]       
    console.log(a.slice(5,3));//[]    
    console.log(a.slice(7));//[]         
    
  • 配列の削除と挿入の置換
  • 削除
  • a=[1,2,3,4,5,6,7];
    aa=a.splice(1,2);//     ,    
    console.log(a);//[1,4,5,6,7]     a
    console.log(aa);//[2,3]      
    
  • 挿入
  • a=[1,2,3,4,5,6,7];
    aa=a.splice(1,0,"7","8","9");//               ,         
    console.log(a);//[1,"7","8","9",2,3,4,5,6,7]     a
    console.log(aa);//[] 
    
  • 置換
  • a=[1,2,3,4,5,6,7];
    aa=a.splice(1,1,"7","8","9");//               ,         
    console.log(a);//[1,"7","8","9",3,4,5,6,7]     a,   2   
    console.log(aa);//2   ,       
    
  • 位置方法
  • a=[1,2,3,4,5,4,3,2,1];
    console.log(a.indexOf(2));//1        
    console.log(a.indexOf(2,3));//7         2,     
    console.log(a.lastIndexOf(2));//7         ,     
    console.log(a.lastIndexOf(2,3));//1   
    console.log(a.lastIndexOf(9));//-1        1
    console.log(a.lastIndexOf("2"));//-1              ===
    
    IE 8および以下はindexOfをサポートしていません.次のようにサポートしてもいいです.
    if (!Array.prototype.indexOf){  //                
            Array.prototype.indexOf = function(elt /*, from*/){  
            var len = this.length >>> 0;  
            var from = Number(arguments[1]) || 0;  
            from = (from < 0)  
                 ? Math.ceil(from)  
                 : Math.floor(from);  
            if (from < 0)  
              from += len;  
            for (; from < len; from++)  
            {  
              if (from in this &&  
                  this[from] === elt)  
                return from;  
            }  
            return -1;  
          };  
        }
    
  • ローズマリーIE 9および以上のブラウザ
  • every配列のすべての値が条件を満たしてtrue
  • に戻ります.
    a=[1,2,3,4,5];
    aa=a.every(function(item,index,array){
        console.log(item);//     
        console.log(index);//     
        console.log(array);//    
        return item>=1;//      1   true
    })
    console.log(aa);//true
    aa=a.every(function(item,index,array){
        console.log(item);//     
        console.log(index);//     
        console.log(array);//    
        return item>2;//       2,         
    })
    console.log(aa);//false
    
  • some配列の中で一つの条件が満たされたらtrue
  • に戻ります.
    a=[1,2,3,4,5];
    aa=a.some(function(item,index,array){
        return item>4;//        true
    })
    console.log(aa);//true
    aa=a.every(function(item,index,array){
        return item>6;//     
    })
    console.log(aa);//false
    
  • filterフィルタリング条件に合わないデータ項目
  • a=[1,2,3,4,5];
    aa=a.filter(function(item,index,array){
        return item>3;//            
    })
    console.log(aa);//[4,5]
    
  • mapは、各データ項目に対して動作し、
  • に戻る.
    a=[1,2,3,4,5];
    aa=a.map(function(item,index,array){
        return item*3;
    })
    console.log(aa);//[3,6,9,12,15]
    aa=a.map(function(item,index,array){
        return item>3;
    })
    console.log(aa);//[false,false,false,true,true]
    
  • forEachサイクルの値は、戻り値なしであり、for方法
  • と同様である.
    a=[1,2,3,4,5];
    a.forEach(function(item,index,array){
        console.log(item);
        console.log(index);
        console.log(array);
    })
    
  • reduce循環結合方法
  • a=[1,2,3,4,5];
    aa=a.reduce(function(pre,cur,index,array){
        console.log(pre+"   "+cur+"   "+index);pre   return        ,cur             
        return pre+cur;
    })
    console.log(aa);//15            
    //   reduceRight  ,