JavaScript native has not provided the deletion function, so he wrote a few remove methods, mainly to note that the use of the splice method to iterate through the array when the traversal is not completed will change the length of the array object, the simplest way is to start from the end of the array to iterate, with the decrement to loop , like my lastrmove in the comments section, this method is intuitive and not affected by the length of the dynamic changes, and then I would like to be like Undescore as well as the order and matching to the first return to this function, so made a few changes, You can either return from the first or the end of a match, or traverse the entire array.
//iterate over the entire array, remove the element that matches the item, use the strong comparison = = =, give the second argument to the beginning to find the first match the item element is removed and returned;//If an element is found to return the processed array itself, return to undefined if it has not been found;Array.prototype.Remove =function(item, all) {varresult, Istype = Object.prototype.toString, I, Len, start, haslast = arguments[2]; Start= 0, Len = This. Length; for(i = start; I <Len;) { varIspass =true, Inx; if(!haslast) {Inx=i; } Else{inx= Len-1; } if(Istype.call (item) = = ' [Object Array] ') { for(varII = 0, Iimax = item.length; II < Iimax; ii++) { if( This[Inx] = = =Item[ii]) {Ispass=false; Break; } } } Else if( This[Inx] = = =Item) {Ispass=false; } if(!Ispass) {Result=true; This. Splice (Inx, 1); if(All) { Break; } } Else if(!haslast) {Len= This. Length; I++; } Else{len--; } } returnResult? This:void0;}//with the above Rmove, start looking from the tail, find and delete the first match immediately after the return;//If an element is found to return the processed array itself, return to undefined if it has not been found;Array.prototype.LastRemove =function(item) {/*var result = [], istype = object.prototype.tostring.call,isfrist; for (var i = this.length-1; I >= 0; i--) {var ispass = true; if (Object.prototype.toString.call (item) = = ' [Object Array] ') {for (var II = 0, Iimax = item.length; II < Iimax; I i++) {if (this[i] = = = Item[ii]) {ispass = false; Break }}}} else if (this[i] = = = Item) {Ispass = false; } if (!ispass) {if (isfrist &&!all) {break; } isfrist = true; This.splice (i, 1); } }*/ return This. Remove (Item,true,true);}//The effect is the same as above, traversing the entire array, the difference is that the return is a new array, is a reference to the original array;Array.prototype.RemoveAt =function(item) {varresult = [], Istype =Object.prototype.toString, Ispass, Val; for(varInx = 0, Len = This. length; Inx < Len; inx++) {Ispass=true; Val= This[Inx]; if(Istype.call (item) = = ' [Object Array] ') { for(varII = 0, Iimax = item.length; II < Iimax; ii++) { if(val = = =Item[ii]) {Ispass=false; Break; } } } Else if(val = = =Item) {Ispass=false; } if(Ispass) {Result.push (val); } } returnresult; }
The Remove method for JavaScript arrays