Home > Web Front-end > JS Tutorial > js uses Array.splice to implement Array's insert/remove_javascript skills

js uses Array.splice to implement Array's insert/remove_javascript skills

WBOY
Release: 2016-05-16 18:56:38
Original
1323 people have browsed it

arrayObj.splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])
Parameters
arrayObj
Required. An Array object.
start
Required. Specifies the starting position to remove elements from the array, starting from 0.
deleteCount
Required. The number of elements to remove.
item1, item2,. . .,itemN
Required. A new element to be inserted at the location of the removed element.
Description
The splice method can modify arrayObj by removing a specified number of elements starting from the start position and inserting new elements. The return value is a new Array object consisting of the removed elements.
Requires
version 5.5

Copy code The code is as follows:

Array.prototype .clear=function(){
this.length=0;
}
Array.prototype.insertAt=function(index,obj){
this.splice(index,0,obj);
}
Array.prototype.removeAt=function(index){
this.splice(index,1);
}
Array.prototype.remove=function(obj){
var index=this.indexOf(obj);
if (index>=0){
this.removeAt(index);
}
}

Use :
Copy code The code is as follows:

var a = [];
for ( var i = 0; i < 5; i ) a.insertAt(i, i);
alert(a);
a.removeAt(1);
alert(a);
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template