Home > Web Front-end > JS Tutorial > Javascript deletes array elements based on specified subscript or object_javascript skills

Javascript deletes array elements based on specified subscript or object_javascript skills

WBOY
Release: 2016-05-16 17:45:55
Original
1450 people have browsed it

Place the following code in the global js file:
Js code

Copy the code The code is as follows:

/**
*Delete the specified subscript or object from the array
*/
Array.prototype.remove=function(obj){
for(var i =0;i var temp = this[i];
if(!isNaN(obj)){
temp=i;
}
if(temp == obj){
for(var j = i; j this[j]=this[j 1];
}
this.length = this.length-1;
}
}
}

Usage:
Js code
Copy code The code is as follows:

var arr =new Array();
arr[0]="dddddd";
arr[1]="dxxxxxxx";
arr[2]="vvvvvvv ";
arr[3]="dbbbbb";
var str ="vvvvvvv";
arr.remove(3);//Delete the object with index 3
arr.remove( str); //Delete object value is "vvvvvvv"
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