Skip to content

zipObject() function

keysvalues を位置ごとにペアにしてオブジェクトを作ります。対応する値がないキーは undefined になり、余分な値は無視されます。

Signature:

typescript
function zipObject<K extends PropertyKey, V>(
  keys: Iterable<K>,
  values: Iterable<V>,
): Record<K, V>;

Example

ts
zipObject(["a", "b"], [1, 2]); // { a: 1, b: 2 }
zipObject(["a", "b", "c"], [1, 2]); // { a: 1, b: 2, c: undefined }
zipObject(["a", "b"], [1, 2, 3]); // { a: 1, b: 2 }

Open Source Code

Released under the Apache-2.0 License.