Method One: Filter ES5 method
var a=[1,5, ' ff ', ' g ', ' h ', ' SD '];alert (remove (A, ' G ')); function Remove (arr,item) { return arr.filter (function (ele) {return ele! =Item})}
Method Two: Push method
function Remove (arr,item) { var arr2=[]; for (var i=0;i<arr.length; i++) { if (arr[i]!= Item) { arr2.push (arr[i]); } } return arr2;}
Method Three: Splice method
// The trick of this method is to delete the equal element item backwards, avoiding the problem of moving the element forward when it is deleted; function Remove (arr,item) { var newarr=arr.slice (0); for (var i=newarr.length;i>=0; i-- ) { if (newarr[i]==Item) { Newarr.splice (i,1); } } return newarr;}
Method Four: Using the characteristics of the array itself, the array as a queue, using the push and shift methods to operate;
function Remove (Arr,item) {
var newarr=arr.slice (0);
for (var i=0;i<newarr.length+1;i++) //Note i<newarr.length+1; here
{
if (Newarr[0]!==item)
{
Newarr.push (Newarr[0]);
Newarr.shift ();
}
else{
Newarr.shift ();
}
}
return newarr;
}
3 Move the divisor group all elements in arr that are equal to item, and do not change the original array.