js Array 阵列扩展方法

it2026-05-17  23

//又来了 Array.prototype.unique = function() { this.sort(); var re=[this[0]]; for(var i = 1; i < this.length; i++) { if( this[i] !== re[re.length-1]) { re.push(this[i]); } } return re; } //并集 Array.prototype.union = function(a) { return this.concat(a).unique(); } //差集 Array.prototype.minus = function(a) { var result =[]; var clone = this; for(var i=0; i < clone.length; i++) { var flag = true; for(var j=0; j < a.length; j++) { if(clone[i] == a[j]) flag = false; } if(flag) result.push(clone[i]); } return result.unique(); } // 交集 Array.prototype.intersect = function(b) { var result = []; var a = this; for(var i = 0; i < b.length; i ++) { var temp = b[i]; for(var j = 0; j < a.length; j ++) { if(temp === a[j]) { result.push(temp); break; } } } return result.unique(); }

版权声明:本文博主原创文章。博客,未经同意不得转载。

转载于:https://www.cnblogs.com/bhlsheji/p/4891510.html

最新回复(0)