웹 개발/스크립트 일반
자바스크립트 Array remove
팩트폭력배
2013. 8. 28. 10:06
● 값으로 지우기
Array.prototype.removeByValue = function() { if(!Array.prototype.indexOf) { Array.prototype.indexOf = function(what, i) { i = i || 0; var L = this.length; while (i < L) { if(this[i] === what) return i; ++i; } return -1; }; } var what, a = arguments, L = a.length, ax; while (L && this.length) { what = a[--L]; while ((ax = this.indexOf(what)) !== -1) { this.splice(ax, 1); } } return this; };
● 인덱스로 지우기
Array.prototype.removeByIdx = function(idx) { return (idx<0 || idx>this.length) ? this : this.slice(0, idx).concat(this.slice(idx+1, this.length)); };