javascript - Recursively add identical element to each object in array -


i have object contain array of object ('pages')

var obj = {     "pages" : [         {                "title": "title one",             "sub" : [                 { "title" : "title 1 one"},                 { "title" : "title 1 two"}             ]         },         { "title" : "title two" },         {              "title" : "title three",             "sub" : [                 { "title" : "title 3 one"},                 { "title" : "title 3 two"},                 { "title" : "title 3 three"}             ]         }     ] } 

and expected result (add 'slug' element each object in 'pages')

{     "pages" : [         {             "title" : "title one",             "slug" : "title-one",             "sub" : [                  {                     "title" : "title 1 one",                     "slug" : "title-one-one"                 },                 {                     "title" : "title 1 two",                     "slug" : "title-one-two"                 }             ]         },         {             "title" : "title two",             "slug" : "title-two"         },         {             "title" : "title three",             "slug"  : "title-three",             "sub" : [                 {                     "title" : "title 3 one",                     "slug" : "title-three-one"                 },                 {                     "title" : "title 3 two",                     "slug" : "title-three-two"                 },                 {                     "title" : "title 3 three",                     "slug" : "title-three-three"                 }             ]         }     ] } 

i guess can like

function add_slug(array_of_obj) {     (var item in array_of_obj) {          //i not sure this:         item.slug = item.title.replace(/ /g, '-');          if(typeof item.sub !== undefined) {             add_slug(item.sub);         }     } }  add_slug(obj.pages); 

but undefined item.title keep modifying function doesn't work yet need help. thanks.

changed use loop rather for-in - safer array.

try this:

function add_slug(array_of_obj) {     for(var pageidx = 0; pageidx < array_of_obj.length; ++pageidx) {         var curpage = array_of_obj[pageidx];          if(curpage.title) {             curpage.slug = curpage.title.split(' ').join('-');         }          if(curpage.sub) {             add_slug(curpage.sub);         }     } } 

result:

{   "pages": [     {       "title": "title one",       "sub": [         {           "title": "title 1 one",           "slug": "title-one-one"         },         {           "title": "title 1 two",           "slug": "title-one-two"         }       ],       "slug": "title-one"     },     {       "title": "title two",       "slug": "title-two"     },     {       "title": "title three",       "sub": [         {           "title": "title 3 one",           "slug": "title-three-one"         },         {           "title": "title 3 two",           "slug": "title-three-two"         },         {           "title": "title 3 three",           "slug": "title-three-three"         }       ],       "slug": "title-three"     }   ] }  

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -