废话不多说,请看代码:
方法一:
var a = ["a", "b", "c", "d"];
var b = a.slice();
b=b.splice(1);
console.log(a); //a=(4) ["a", "b", "c", "d"]
console.log(b); //b=(3)["b","c","d"]
jquery中,slice(start,stop)方法用户选取基于索引的元素的子集。
1.slice(): 选取全部元素。上述例子中,var b = a.slice(); b=["a", "b", "c", "d"];
2.slice(1):选取从索引1开始的所有元素。以上述例子为例,var b = a.slice(1);b=["b","c","d"];
3.slice(0,2): 选取从索引0开始,到索引2结尾的元素。以上述例子为例,var b = a.slice(0,2);b=["a","b","c"];
4.slice(-1): 当值为负数时,表示从被选元素的末端选取元素。以上述例子为例,var b = a.slice(-1);b=["d"];
5.slice(-3,-1): 当值为负数时,表示从被选元素的末端选取元素。注意:当start值为负数时,stop值必须也为负数,且stop值大于start值,否则选取失败。以上述例子为例,var b = a.slice(-3,-1);b=["b","c"];
(这里我有点小问题,麻烦大家解答一下:第4点中,a.slice(-1)=["d"],但是第5点中,a.slice(-3,-1) = ["b","c"],em,,请大家指点一下,谢谢你们)
这里的splice,(下班再总结吧,还在搬砖呢,不好意思大家)
方法二:
var a = ["a", "b", "c", "d"];
var b = a.concat();
b=b.splice(1);
console.log(a); //a=(4) ["a", "b", "c", "d"]
console.log(b); //b=(3)["b","c","d"]