From 047ff76e3a51c2b57ca4d65c047e9080eb17c2f1 Mon Sep 17 00:00:00 2001 From: zhl Date: Fri, 8 Jan 2021 17:39:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A03=E4=B8=AA=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E6=B1=82=E5=B9=B6=E9=9B=86,=E4=BA=A4=E9=9B=86=E5=92=8C?= =?UTF-8?q?=E5=B7=AE=E9=9B=86=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/Extend.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/common/Extend.ts b/src/common/Extend.ts index 6173789..9d82a79 100644 --- a/src/common/Extend.ts +++ b/src/common/Extend.ts @@ -563,6 +563,23 @@ interface Array { * @param n n > 0 右移, n<0 左移 */ moveElement?(n: number): T[]; + + /** + * 两个数组并集 + * @param arr + */ + union?(arr: T[]): T[]; + + /** + * 两个数组交集 + * @param arr + */ + intersect?(arr: T[]): T[]; + /** + * 相对于arr的差集 + * @param arr + */ + difference?(arr: T[]): T[]; } Object.defineProperties(Array.prototype, { @@ -815,6 +832,32 @@ Object.defineProperties(Array.prototype, { return this.slice(-n).concat(this.slice(0, -n)); }, writable: true + }, + + union: { + value: function (this: T[], b: any[]): T[] { + let a = this.concat(b); + return [...new Set(a)]; + }, + writable: true + }, + + intersect: { + value: function (this: T[], b: any[]): T[] { + let set0 = new Set(b); + let set1 = new Set(this.filter (x => set0.has(x))); + return [...set1]; + }, + writable: true + }, + + difference: { + value: function (this: T[], b: any[]): T[] { + let set0 = new Set(b); + let set1 = new Set(this.filter(x => !set0.has(x))); + return [...set1]; + }, + writable: true } });