<body>
<script>
var arr = [1,2,3,4,5,6];
console.log('arr.slice(2,4)');
</script>
</body>
结果:arr.slice(2,5)
(3) [3, 4, 5]
splice添加:
<body>
<script>
var arr = [1,2,3,4,5,6,7,8];
arr.splice(2,0,'js');
console.log(arr);
</script>
</body>
结果:(9) [1, 2, "js", 3, 4, 5, 6, 7, 8]
splice删除:
<body>
<script>
var arr = [1,2,3,4,5,6,7,8];
arr.splice(2,2);
console.log(arr);
</script>
</body>
结果:(6) [1, 2, 5, 6, 7, 8]
splice更改:
<body>
<script>
var arr = [1,2,3,4,5,6,7,8];
arr.splice(2,1,10);
console.log(arr);
</script>
</body>
结果:(8) [1, 2, 10, 4, 5, 6, 7, 8]
重点:1、slice用于取出数组中相应的数值,如arr.slice(1,3),取出数组arr中第2个到第3个数值元素!
2、splice可以用来添加删除数组元素;