配列を操作するいくつかの方法

2186 ワード



	
		
		
	
	
	

<script type="text/javascript">
	1.shift
//         ,         ,         undefined
var a=[1,2,3,4,5];
var b=a.shift();//a:[2,3,4,5] b:1
2.unshift
//           ,        
var a=[1,2,3,4,5];
var b=a.unshift(-2,-1);//a;[-2,-1,1,2,3,4,5] b:7// :    ie6.0 
//         undefined, firefox        7,        
//           splice  
3.pop
//          ,         ;         undefined
var a=[1,2,3,4,5]
var b=a.pop();//a:[1,2,3,4] b:5
4.push
//           ,        
var a=[1,2,3,4,5];
var b=a.push(6,7);//a:[1,2,3,4,5,6,7] b:7
5.concat
//       ,              
var a=[1,2,3,4,5]
var b=a.concat(6,7)//a:[1,2,3,4,5] b:[1,2,3,4,5,6,7]
6.splice
//splice(start,deleteCount.val1,val2,...): start    deleteCount 
//        val1,val2,...
var a=[1,2,3,4,5];
var b=a.splice(2,2,7,8,9)//a:[1,2,7,8,9,5] b:[3,4]
7.reverse
//     
var a=[1,2,3,4,5]
var b=a.reverse();//a:[5,4,3,2,1] b:[5,4,3,2,1]
8.sort(orderfunction)
//             
var a=[1,2,3,4,5]
var b=a.sort();//a:[1,2,3,4,5] b:[1,2,3,4,5]
9.slice(start,end)
//                            
//      
var a=[1,2,3,4,5]
var b=a.slice(2,5);//a:[1,2,3,4,5] b:[3,4,5]

10.join(separator)
//             , separator    ,  
//             
var a=[1,2,3,4,5]
var b=a.join("|");//a:[1,2,3,4,5] b:"1|2|3|4|5"
11.indexOf
//             ,     -1,   0  
var a = ['a','b','c','d','e']; 
a.indexOf('a');//0
a.indexOf(a);//-1
a.indexOf('f');//-1
a.indexOf('e');//4
</script>

       
shift:              
unshift:         
pop:               
splice:                   N       
join:         
push:                  
reverse:    
slice:(start,end)    
indexOf:                  
concat:          
sort:    
<!--  -->
var arr = [4,3,6,5,7,2,1];
arr.sort();
console.log(arr);
//    [1,2,3,4,5,6,7]
<!--  -->
var arr = [4,3,6,5,7,2,1];
arr.sort();
arr.sort(function(a,b){
    return b-a;
});
console.log(arr);
//    [7,6,5,4,3,2,1]
</code></pre> 
  <p> </p> 
 </div> 
</div>
                            </div>
                        </div>