-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(zipObject): Add zipObject function (#92)
* 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>
- Loading branch information
Showing
6 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { bench, describe } from 'vitest'; | ||
import { zipObject as zipObjectToolkit } from 'es-toolkit'; | ||
import { zipObject as zipObjectLodash } from 'lodash'; | ||
|
||
describe('zipObject', () => { | ||
bench('es-toolkit/zipObject', () => { | ||
zipObjectToolkit([1, 2, 3, 4], ['a', 'b', 'c', 'd']); | ||
}); | ||
|
||
bench('lodash/zipObject', () => { | ||
zipObjectLodash([1, 2, 3, 4], ['a', 'b', 'c', 'd']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# zipObject | ||
|
||
두 배열을 하나의 객체로 결합해요. 첫 번째 배열은 프로퍼티 이름을 나타내고, 두 번째 배열은 값을 나타내요. | ||
|
||
프로퍼티 이름을 나타내는 배열이 값을 나타내는 배열보다 길면, 값들은 `undefined`로 채워져요. | ||
|
||
## 인터페이스 | ||
|
||
```typescript | ||
function zipObject<P extends string | number | symbol, V>(keys: P[], values: V[]): { [K in P]: V }; | ||
``` | ||
|
||
### 파라미터 | ||
|
||
- `keys` (`P[]`): 속성 이름이 포함된 배열이에요. | ||
- `values` (`V[]`): 속성 이름에 대응되는 값이 포함된 배열이에요. | ||
|
||
### 반환 값 | ||
|
||
(`{ [K in P]: V }`): 주어진 속성 이름과 값으로 구성된 새로운 객체예요. | ||
|
||
## 예시 | ||
|
||
```typescript | ||
const keys = ['a', 'b', 'c']; | ||
const values = [1, 2, 3]; | ||
const result = zipObject(keys, values); | ||
// result는 { a: 1, b: 2, c: 3 }가 돼요. | ||
|
||
const keys2 = ['a', 'b', 'c']; | ||
const values2 = [1, 2]; | ||
const result2 = zipObject(keys2, values2); | ||
// result2는 { a: 1, b: 2, c: undefined }가 돼요. | ||
|
||
const keys3 = ['a', 'b']; | ||
const values3 = [1, 2, 3]; | ||
const result3 = zipObject(keys3, values3); | ||
// result3는 { a: 1, b: 2 }가 돼요. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# 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 | ||
|
||
```typescript | ||
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 | ||
|
||
```typescript | ||
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 } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { zipObject } from './zipObject'; | ||
|
||
describe('zipObject', () => { | ||
it('creates an object from two arrays of keys and values', () => { | ||
expect(zipObject(['a', 'b', 'c'], [1, 2, 3])).toEqual({ a: 1, b: 2, c: 3 }); | ||
|
||
expect(zipObject(['a', 'b', 'c'], [1, 2])).toEqual({ a: 1, b: 2, c: undefined }); | ||
|
||
expect(zipObject(['a', 'b'], [1, 2, 3])).toEqual({ a: 1, b: 2 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* 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. | ||
* | ||
* @param {P[]} keys - An array of property names. | ||
* @param {V[]} values - 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. | ||
* | ||
* @example | ||
* 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 keys2 = ['a', 'b']; | ||
* const values2 = [1, 2, 3]; | ||
* const result2 = zipObject(keys2, values2); | ||
* // result2 will be { a: 1, b: 2 } | ||
*/ | ||
export function zipObject<P extends string | number | symbol, V>(keys: P[], values: V[]): { [K in P]: V } { | ||
const result = {} as { [K in P]: V }; | ||
|
||
for (let i = 0; i < keys.length; i++) { | ||
result[keys[i]] = values[i]; | ||
} | ||
|
||
return result; | ||
} |