Home > Web Front-end > JS Tutorial > 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
Release: 2016-05-16 16:56:36
Original
1906 people have browsed it

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
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