Home  >  Article  >  Web Front-end  >  Javscript deletes the specified element in the array and returns a new array_javascript tips

Javscript deletes the specified element in the array and returns a new array_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:56:361834browse

To delete a value in the array and return a new array, you need to traverse the old array to find the element to be deleted

Copy code The code is as follows :

/*
* Delete the specified value in the array
*/
Array.prototype.remove=function(value){
var len = this.length;
for(var i=0,n=0;iif(this[i]!=value){
this[n ]=this[i];
}else{
console.log(i);//used for testing
}
}
this.length = n;
};

var arr = ['1','2','3','5','2','1','4','2','2'];
arr.remove(2);
console.log(arr);

Javscript deletes the specified element in the array and returns a new array_javascript tips
Statement:
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