반응형
Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 리눅스
- unittest
- node
- react
- fastapi
- MachineLearning
- 맥
- xcode
- webpack
- Chrome
- pydantic
- 개발
- IOS
- 센토스
- build
- localserver
- androidstudio
- vsCode
- linux
- ReactNative
- 네트워크
- TensorFlow
- 티스토리챌린지
- centos
- 오블완
- VirtualBox
- MAC
- PYTHON
- Android
Archives
- Today
- Total
로메오의 블로그
[재귀함수] 트리 구조를 리스트 구조로 변환 본문
반응형
트리구조 데이터 준비
var data = {
id: '1',
children: [
{
id: '2',
children: [
{
id: '4',
children: [
{
id: '5'
},
{
id: '6'
}
]
},
{
id: '7'
}
]
},
{
id: '3',
children: [
{
id: '8'
},
{
id: '9'
}
]
}
]
};
재귀함수
var array = []
var getAllRoles = item => {
array.push(item);
if (item.children) {
return item.children.map(i => getAllRoles(i));
}
}
getAllRoles(data)
console.log(array)
>> (9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
반응형
'Frontend > ETC' 카테고리의 다른 글
| lodash 자주 사용하는 메서드 정리 (0) | 2021.11.16 |
|---|---|
| [Typescript] getter setter (0) | 2021.11.04 |
| [POSTMAN] 팀 워크 스페이스 생성 [Team workspaces] (0) | 2021.11.03 |
| 반복문 도중에 종료하기 [for of, lodash, array, 배열] (0) | 2021.11.02 |
| typescript lint 체크 제외하기 (0) | 2021.04.09 |
Comments