如果不允许添加到本机原型,则可以是全局函数或自定义对象的方法。它从数组中删除与任何参数匹配的所有项目。
Array.prototype.remove = function() { 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;};var ary = ['three', 'seven', 'eleven'];ary.remove('seven');
为了使其成为全球性的
function removeA(arr) { var what, a = arguments, L = a.length, ax; while (L > 1 && arr.length) { what = a[--L]; while ((ax= arr.indexOf(what)) !== -1) { arr.splice(ax, 1); } } return arr;}var ary = ['three', 'seven', 'eleven'];removeA(ary, 'seven');
并照顾IE8及以下版本-
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; };}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)