1、.concat() 方法
例一:
var a = [1,2,3]
console.log(a.concat(4,5)); //1,2,3,4,5
例二:
var arr = new Array(3)
arr[0] = "aaa"
arr[1] = "bbb"
arr[2] = "ccc"
var arr1 = new Array(3)
arr2[0] = "ddd"
arr2[1] = "eee"
arr2[2] = "fff"
console.log(arr.concat(arr1)); //aaa,bbb,ccc,ddd,eee,fff
2、.slice() 方法
slice()通过索引位置获取新的数组,该方法不会修改原数组,只是返回一个新的数组。
例一:获取数组最后一个元素
var arr = [1,2,3,4];
arr.slice(-1); //4
例二:获取数组的前三个元素
var arr = [1,2,3,4];
arr.slice(0,-1); //[1,2,3]
例三:获取数组除第一个元素外的其他元素
var arr = [1,2,3,4];
arr.slice(1); //[2,3,4]
例四:
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
fruits.slice(1,3); //Orange,Lemon
3、splice() 方法
例一:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,0,"Lemon","Kiwi"); //Banana,Orange,Lemon,Kiwi,Apple,Mango
例二:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,1,"Lemon","Kiwi"); //Banana,Orange,Lemon,Kiwi,Mango
|