Skip to main content

.of and .from methods on collection constructors

Less than 1 minutefeaturees-proposal

.of and .from methods on collection constructorsopen in new window

Modules

Types

interface SetConstructor {
  of<T>(...args: Array<T>): Set<T>;
  from<T, U>(
    iterable: Iterable<T>,
    mapFn?: (value: T, index: number) => U,
    thisArg?: any
  ): Set<U>;
}
interface MapConstructor {
  of<K, V>(...args: Array<[K, B]>): Map<K, V>;
  from<K, V, T>(
    iterable: Iterable<T>,
    mapFn?: (value: T, index: number) => [key: K, value: V],
    thisArg?: any
  ): Map<K, V>;
}

interface WeakSetConstructor {
  of<T extends object>(...args: Array<T>): WeakSet<T>;
  from<T, U extends object>(
    iterable: Iterable<T>,
    mapFn?: (value: T, index: number) => U,
    thisArg?: any
  ): WeakSet<U>;
}

interface WeakMapConstructor {
  of<K extends object, V>(...args: Array<[K, V]>): WeakMap<K, V>;
  from<K extends object, V, T>(
    iterable: Iterable<T>,
    mapFn?: (value: T, index: number) => [key: K, value: V],
    thisArg?: any
  ): WeakMap<K, V>;
}

Entry points

core-js/proposals/collection-methods
core-js/proposals/collection-of-from
core-js(-pure)/full/set/from
core-js(-pure)/full/set/of
core-js(-pure)/full/map/from
core-js(-pure)/full/map/of
core-js(-pure)/full/weak-set/of
core-js(-pure)/full/weak-set/from
core-js(-pure)/full/weak-map/of
core-js(-pure)/full/weak-map/from

Example

Exampleopen in new window:

Set.of(1, 2, 3, 2, 1); // => Set {1, 2, 3}

Map.from(
  [
    [1, 2],
    [3, 4],
  ],
  ([key, value]) => [key ** 2, value ** 2]
); // => Map { 1: 4, 9: 16 }