Javscript removes the specified element from the array and returns a new array
- 2020-03-30 02:14:23
- OfStack
To delete a value from an array and return a new array, you need to traverse the old array to find the element to delete
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201403/201403061736474.gif? 20142617375 ">
Array.prototype.remove=function(value){
var len = this.length;
for(var i=0,n=0;i<len;i++){//Assign the element to be deleted to the new array
if(this[i]!=value){
this[n++]=this[i];
}else{
console.log(i);//For testing
}
}
this.length = n;
};
var arr = ['1','2','3','5','2','1','4','2','2'];
arr.remove(2);
console.log(arr);
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201403/201403061736474.gif? 20142617375 ">