添加3个数组求并集,交集和差集的方法
This commit is contained in:
parent
0f5bd18304
commit
047ff76e3a
@ -563,6 +563,23 @@ interface Array<T> {
|
|||||||
* @param n n > 0 右移, n<0 左移
|
* @param n n > 0 右移, n<0 左移
|
||||||
*/
|
*/
|
||||||
moveElement?<T>(n: number): T[];
|
moveElement?<T>(n: number): T[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 两个数组并集
|
||||||
|
* @param arr
|
||||||
|
*/
|
||||||
|
union?<T>(arr: T[]): T[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 两个数组交集
|
||||||
|
* @param arr
|
||||||
|
*/
|
||||||
|
intersect?<T>(arr: T[]): T[];
|
||||||
|
/**
|
||||||
|
* 相对于arr的差集
|
||||||
|
* @param arr
|
||||||
|
*/
|
||||||
|
difference?<T>(arr: T[]): T[];
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperties(Array.prototype, {
|
Object.defineProperties(Array.prototype, {
|
||||||
@ -815,6 +832,32 @@ Object.defineProperties(Array.prototype, {
|
|||||||
return this.slice(-n).concat(this.slice(0, -n));
|
return this.slice(-n).concat(this.slice(0, -n));
|
||||||
},
|
},
|
||||||
writable: true
|
writable: true
|
||||||
|
},
|
||||||
|
|
||||||
|
union: {
|
||||||
|
value: function<T> (this: T[], b: any[]): T[] {
|
||||||
|
let a = this.concat(b);
|
||||||
|
return [...new Set(a)];
|
||||||
|
},
|
||||||
|
writable: true
|
||||||
|
},
|
||||||
|
|
||||||
|
intersect: {
|
||||||
|
value: function<T> (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<T> (this: T[], b: any[]): T[] {
|
||||||
|
let set0 = new Set(b);
|
||||||
|
let set1 = new Set(this.filter(x => !set0.has(x)));
|
||||||
|
return [...set1];
|
||||||
|
},
|
||||||
|
writable: true
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user