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.