配列slice()方法とsplice()方法

1022 ワード

/**
*slice()        ,       ,       
*           ,                
*                  ,      ,         
*/
var arr = [1,2,3,4,5,6,7,8,9,0];
console.log(arr.slice(1))//[2, 3, 4, 5, 6, 7, 8, 9, 0]
console.log(arr.slice(1,4))//[2, 3, 4]
console.log(arr)// [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
/**
*slice()            ,         
* slice(),           
*                     +                  ,     
*/
console.log(arr.slice(-4,-2))//[7, 8] .....       10-4      10-2  
console.log(arr.slice(-2,-4))//[] .....         10-2       10-4    
/**
* splice()              
*               ,     ,       ,      ,     ,         
*/
var arr1 = [1,2,3,4,5,6];//        
console.log(arr1.splice(0,1))//[1]            
console.log(arr1)//[2, 3, 4, 5, 6]           

var arr2 = [1,2,3,4,5,6];//        
console.log(arr2.splice(6,0,7,8,9))//[]
console.log(arr2)//[1, 2, 3, 4, 5, 6, 7, 8, 9]

var arr3 = [1,2,3,4,5,6];//             ,         
console.log(arr3.splice(1,2,7,8))//[2, 3]
console.log(arr3)//[1, 7, 8, 4, 5, 6]