Skip to content
This repository was archived by the owner on Dec 18, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all 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
23 changes: 23 additions & 0 deletions packages/common/utils/src/array/flattenArray.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# flattenArray

이차원 배열을 일차원 배열로 반환합니다.

## Example

```typescript
// [1, 2, 3, 4]
flattenArray([
[1, 2],
[3, 4],
]);

// [{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }]
flattenArray([
[{ a: 1 }, { b: 2 }],
[{ c: 3 }, { d: 4 }],
]);

// 일차원 배열을 입력했을 경우 입력받은 배열을 다시 그대로 반환합니다.
// 1, 2, 3, 4
flattenArray([1, 2, 3, 4]);
```
23 changes: 23 additions & 0 deletions packages/common/utils/src/array/flattenArray.ko.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# flattenArray

이차원 배열을 일차원 배열로 반환합니다.

## Example

```typescript
// [1, 2, 3, 4]
flattenArray([
[1, 2],
[3, 4],
]);

// [{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }]
flattenArray([
[{ a: 1 }, { b: 2 }],
[{ c: 3 }, { d: 4 }],
]);

// 일차원 배열을 입력했을 경우 입력받은 배열을 다시 그대로 반환합니다.
// 1, 2, 3, 4
flattenArray([1, 2, 3, 4]);
```
28 changes: 28 additions & 0 deletions packages/common/utils/src/array/flattenArray.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { flattenArray } from '.';

describe('flattenArray', () => {
it('should work such that if a two-dimensional array is input, it returns a one-dimensional array', () => {
const twoDimensionsArray1 = [
[1, 2],
[3, 4],
];
expect(flattenArray(twoDimensionsArray1)).toEqual([1, 2, 3, 4]);

const twoDimensionsArray2 = [
[{ a: 1 }, { b: 2 }],
[{ c: 3 }, { d: 4 }],
];
expect(flattenArray(twoDimensionsArray2)).toEqual([{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }]);

const twoDimensionsArray3 = [
['', false, 0, undefined, null],
[null, undefined, 0, false, ''],
];
expect(flattenArray(twoDimensionsArray3)).toEqual(['', false, 0, undefined, null, null, undefined, 0, false, '']);
});

it('should work by returning the array as is if a one-dimensional array is input', () => {
const oneDimensionsArray = [1, 2, 3, 4];
expect(flattenArray(oneDimensionsArray)).toEqual([1, 2, 3, 4]);
});
});
13 changes: 13 additions & 0 deletions packages/common/utils/src/array/flattenArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/** @tossdocs-ignore */
export function flattenArray<Type>(array: Type[]): Type[];
export function flattenArray<Type>(array: Type[][]): Type[] {
if (array.length === 0) {
return array as Type[] & Type[][];
}

if (Array.isArray(array[0])) {
return (array as Type[][]).reduce((acc: Type[], curr: Type[]) => acc.concat(curr), []);
}

return array as Type[] & Type[][];
}
2 changes: 1 addition & 1 deletion packages/common/utils/src/array/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/** @tossdocs-ignore */
export * from './arrayIncludes';
export * from './flattenArray';
export * from './isDifferentArray';
export * from './isNonEmptyArray';
export * from './last';
export * from './NonEmptyArray';
export * from './sample';