Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zipObject): Add zipObject function #92

Merged
merged 6 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions benchmarks/zipObject.bench.ts
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']);
});
});
39 changes: 39 additions & 0 deletions docs/ko/reference/array/zipObject.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# zipObject

두 개의 배열을 속성 이름과 해당 값의 객체로 결합해요.

이 함수는 속성 이름이 포함된 배열과 해당 값이 포함된 배열을 입력받아, 속성 이름 배열의 요소를 키로 하고 값 배열의 요소를 값으로 하는 새로운 객체를 반환해요. 속성 이름 배열이 값 배열보다 길 경우, 남은 속성 이름의 값은 `undefined`로 채워져요.
raon0211 marked this conversation as resolved.
Show resolved Hide resolved

## 인터페이스

```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 }가 돼요.
```
39 changes: 39 additions & 0 deletions docs/reference/array/zipObject.md
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 }
```
1 change: 1 addition & 0 deletions src/array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ export { xorBy } from './xorBy';
export { xorWith } from './xorWith';
export { zip } from './zip';
export { zipWith } from './zipWith';
export { zipObject } from './zipObject';
12 changes: 12 additions & 0 deletions src/array/zipObject.spec.ts
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 });
});
});
37 changes: 37 additions & 0 deletions src/array/zipObject.ts
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;
}