Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. todots = (child, valProp = 'name', nestedProp = 'children', result = {}, path = 'root') => {
  2.   if (!child) {
  3.     return
  4.   }
  5.   result[path] = child
  6.   if (child[nestedProp]) {
  7.     for (let c of child[nestedProp]) {
  8.       todots(c, valProp, nestedProp, result, path + '.' + child[valProp])
  9.     }
  10.   }
  11.   return result
  12. }
  13.  
  14. obj = {
  15.   name: 'child1',
  16.   children: [
  17.     {
  18.       name: 'child of child'
  19.     },
  20.     {
  21.       name: 'child of child',
  22.       children: [
  23.         {
  24.           name: 'child of child'
  25.         }
  26.       ]
  27.     }
  28.   ]
  29. }
  30.  
  31. console.log(Object.keys(todots(obj, 'name', 'children')))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement