JavaScript數組的splice()方法改變數組的內容,增加了新的元素,同時消除舊元素。
語法
array.splice(index, howMany, [element1][, ..., elementN]);
下面是參數的詳細信息:
返回值:
返回基於傳遞的參數提取數組。
例子:
<html> <head> <title>JavaScript Array splice Method</title> </head> <body> <script type="text/javascript"> var arr = ["orange", "mango", "banana", "sugar", "tea"]; var removed = arr.splice(2, 0, "water"); document.write("After adding 1: " + arr ); document.write("<br />removed is: " + removed); removed = arr.splice(3, 1); document.write("<br />After adding 1: " + arr ); document.write("<br />removed is: " + removed); </script> </body> </html>
這將產生以下結果:
After adding 1: orange,mango,water,banana,sugar,tea removed is: After adding 1: orange,mango,water,sugar,tea removed is: banana