mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-28 03:34:26 +03:00
537745077f
* feat(zipObject): Add `zipObject` function that combines keys and values into an object * feat(zipObject): Add `zipObject` function test code * feat(zipObject): Add `zipObject` function bench * feat(zipObject): Add `zipObject` function docs * Update docs/ko/reference/array/zipObject.md --------- Co-authored-by: Sojin Park <raon0211@gmail.com>
1.3 KiB
1.3 KiB
zipObject
Combines two arrays, one of property names and one of corresponding values, into a single object.
This function takes two arrays: one containing property names and another containing corresponding values. It returns a new object where the property names from the first array are keys, and the corresponding elements from the second array are values. If the keys
array is longer than the values
array, the remaining keys will have undefined
as their values.
Signature
function zipObject<P extends string | number | symbol, V>(keys: P[], values: V[]): { [K in P]: V };
Parameters
keys
(P[]
): An array of property names.values
(V[]
): An array of values corresponding to the property names.
Returns
({ [K in P]: V }
): A new object composed of the given property names and values.
Examples
const keys = ['a', 'b', 'c'];
const values = [1, 2, 3];
const result = zipObject(keys, values);
// result will be { a: 1, b: 2, c: 3 }
const keys2 = ['a', 'b', 'c'];
const values2 = [1, 2];
const result2 = zipObject(keys2, values2);
// result2 will be { a: 1, b: 2, c: undefined }
const keys3 = ['a', 'b'];
const values3 = [1, 2, 3];
const result3 = zipObject(keys3, values3);
// result3 will be { a: 1, b: 2 }