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 the code code as follows:
/*
* Delete the specified value in the array
*/
Array.prototype.remove=function(value){
varlen = this.length;
for(var i=0,n=0;i<len;i++){//Assign the elements to be deleted to the new array
if(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);