Array.prototype.remove = function (obj) {
for(var i = 0; i < this.length; i++) {
if(this[i] === obj) {
if (i == this.length) {
this[i] = null;
} else {
for(var j = i; j < this.length-1; j++) {
this[j] = this[j+1];
}
delete this[j]; // updated from this[j] = null; still not working.
}
}
}
return this;
};
appeler avec:Quel est le problème avec ma fonction de suppression?
write("ARRAY TEST = " + [22, 33, 44].remove(33).remove(22));
imprime ..Il:
44,,
Pourquoi deux virgules et comment corriger ma fonction remove pour supprimer les virgules ainsi?
+1 pour tout sauf le * "P.S." *; O) – user113716
Ca marche, ça! J'ai juste trouvé étrange que [ce doc] (http://www.w3schools.com/jsref/jsref_slice_array.asp) dise que le second argument slice est "end", pas "length" tel qu'il est réellement. –
@Tom Pour référence future coller à MDN, w3schools est obsolète et a beaucoup de désinformation. –