Skip to content

fromEntries() function

从字符串键值对返回对象。

Signature:

typescript
declare function fromEntries<
  U extends [Key, any] | readonly [Key, any],
  T extends Iterable<U> | AsyncIterable<U>,
>(
  iterable: T,
): ReturnValueType<
  T,
  {
    [K in IterableInfer<T> as K[0]]: K[1];
  }
>;

Example

ts
const arr = [
  ["a", 1],
  ["b", true],
  ["c", "hello"],
  ["d", { d1: 1, d2: 3 }],
] as (
  | ["a", number]
  | ["b", boolean]
  | ["c", string]
  | ["d", { d1: number; d2: number }]
)[];
fromEntries(arr); // { a: 1, b: true, c: 'hello', d: { d1: 1, d2: 3 } }

see entries

Open Source Code

Released under the Apache-2.0 License.