1.
<script> /*splice(start,len)从那个位置开始删除几项*/ var arr = [1,2,3,4,5,6]; var removed = arr.splice(2,3); alert(arr);//[1,2,6] // 从下标为2的位置往后删除3个 alert(removed)//[3,4,5] /*splice(start,len,...) 当len 为 0 的时候为插入*/ var arr2 = [1,2,3,4,5]; var removed = arr2.splice(2,0,"a","b","c"); //从下标为2的位置删除0个,然后插入"a","b","c" var removed = arr2.splice(2,0,["a","b","c"]); //从下标为2的位置删除0个,然后插入["a","b","c"] alert(arr2); // [1,2,"a","b","c",3,4,5] alert(removed == undefined);//空 /*splice(start,len,...) ,当len不为0的时候*/ var arr3 = ["hello","world","js","java"]; var removed = arr3.splice(1,2,[1,2,3,4,5,6]);//先从下标为1的位置往后删除2个元素,然后添加数组进来 alert(arr3);//["hello","java",1,2,3,4,5,6] alert(removed );//["world","js"] </script>
时间: 2024-11-08 15:40:33