跳至主要內容

新的 Set 方法

小于 1 分钟featurees-proposal

新的 Set 方法open in new window

模块

类型

interface Set<T> {
  difference(other: SetLike<T>): Set<T>;
  intersection(other: SetLike<T>): Set<T>;
  isDisjointFrom<U>(other: SetLike<U>): boolean;
  isSubsetOf<U>(other: SetLike<U>): boolean;
  isSupersetOf<U>(other: SetLike<U>): boolean;
  symmetricDifference(other: SetLike<T>): Set<T>;
  union(other: SetLike<T>): Set<T>;
}

interface SetLike<T> {
  has(value: T): boolean;
  keys(): IterableIterator<T>;
  readonly size: number;
}

入口点

core-js/proposals/set-methods-v2
core-js(-pure)/actual|full/set/difference
core-js(-pure)/actual|full/set/intersection
core-js(-pure)/actual|full/set/is-disjoint-from
core-js(-pure)/actual|full/set/is-subset-of
core-js(-pure)/actual|full/set/is-superset-of
core-js(-pure)/actual|full/set/symmetric-difference
core-js(-pure)/actual|full/set/union

示例

示例open in new window:

new Set([1, 2, 3]).union(new Set([3, 4, 5])); // => Set {1, 2, 3, 4, 5}
new Set([1, 2, 3]).intersection(new Set([3, 4, 5])); // => Set {3}
new Set([1, 2, 3]).difference(new Set([3, 4, 5])); // => Set {1, 2}
new Set([1, 2, 3]).symmetricDifference(new Set([3, 4, 5])); // => Set {1, 2, 4, 5}
new Set([1, 2, 3]).isDisjointFrom(new Set([4, 5, 6])); // => true
new Set([1, 2, 3]).isSubsetOf(new Set([5, 4, 3, 2, 1])); // => true
new Set([5, 4, 3, 2, 1]).isSupersetOf(new Set([1, 2, 3])); // => true