js中array(数组)对象的splice方法的详解

 splice()可向数组删除并加入新的元素。

语法

arrayObject.splice(index,howmany,element1,.....,elementX)

<script type="text/javascript">



var arr = new Array(5)



arr[0] = "Jani"



arr[1] = "Hege"



arr[2] = "Stale"



arr[3] = "Kai Jim"



arr[4] = "Borge"



document.write(arr + "<br />")



arr.splice(2,0,"Lene")



document.write(arr + "<br />")



</script>

输出结果为:

Jani,Hege,Stale,Kai Jim,Borge

Jani,Hege,Lene,Stale,Kai Jim,Borge

  
    
arr.splice( 2 , 1 , " Tove " )

输出结果为:

Jani,Hege,Stale,Kai Jim,Borge

Jani,Hege,Tove,Kai Jim,Borge

  
    
arr.splice( 2 , 3 , " Tove " )

输出结果为:

Jani,Hege,Stale,Kai Jim,Borge

Jani,Hege,Tove

你可能感兴趣的:(splice)