-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatten.ts
36 lines (31 loc) · 1 KB
/
flatten.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* flatten 함수 구현하기. */
const arr1 = [{ a: 1 }, 2, [3, 4]];
const arr2 = [1, 2, [3, [4, [5, 6], 7], [8, 9]]];
type ArgumentArrayType<T> = (T | T[])[];
type RecursiveArrayType<T> = T | RecursiveArrayType<T>[];
type RecursiveArgumentArrayType<T> = (
| RecursiveArrayType<T>
| RecursiveArrayType<T>[]
)[];
/* 1차로 중첩된 배열만 flatten */
const flatten = <T>(array: ArgumentArrayType<T>) => {
return array.reduce((acc: T[], cur: T | T[]) => acc.concat(cur), []);
};
console.log(flatten(arr1)); // [1, 2, 3, 4]
/* 무한히 중첩될 수 있는 배열 flatten */
const flattenRecursive = <T>(array: RecursiveArgumentArrayType<T>) => {
return array.reduce(
(
acc: RecursiveArrayType<T>[],
cur: RecursiveArrayType<T> | RecursiveArrayType<T>[]
) => {
if (Array.isArray(cur)) {
return acc.concat(flattenRecursive(cur));
} else {
return acc.concat(cur);
}
},
[]
);
};
console.log(flattenRecursive(arr2)); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]