Created
September 5, 2018 19:18
-
-
Save shermanluong/d1dec0b6045c44fe68041927e2ae3411 to your computer and use it in GitHub Desktop.
Javascript object nest to array if there is childs
This file contains hidden or 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
| function objToArry(obj, ret=[]) {`` | |
| for(var key1 in obj){ | |
| ret.push(childToArray(obj[key1])) | |
| } | |
| return ret; | |
| } | |
| function childToArray(obj) { | |
| if(JSON.stringify(obj['children']) === "{}") | |
| obj['children'] = []; | |
| else | |
| obj['children'] = objToArry(obj['children']); | |
| return obj; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment