javascript - Recursive JS function misses every second loop -


i have function take format of

u1fsqexd1azmnpl : {     2fndvdkaefad6xq : {         fz0zn6d51tgvqid : {             05e1jsfyvhjlgvp : {}         }     } } 

and change to

u1fsqexd1azmnpl : {     subfields : {         2fndvdkaefad6xq : {             subfields : {                 fz0zn6d51tgvqid : {                     subfields : {                         05e1jsfyvhjlgvp : {                             subfields : {}                         }                     }                 }             }         }     } } 

however, comes out as:

u1fsqexd1azmnpl : {     subfields : {         2fndvdkaefad6xq : {             fz0zn6d51tgvqid : {                 subfields : {                     05e1jsfyvhjlgvp : {}                 }             }            }     } } 

note: misses second 'subfields' step.

it every second loop. ie: next id added 05e1jsfyvhjlgvp inside of it, without subfields parent, have subfields child inside of it.

my method is:

        function get_into_subfields(form_structure) {             for(var mainid in form_structure) {                 if(!form_structure[mainid].hasownproperty('subfields')) {                     form_structure[mainid]['subfields'] = {};                 }                 for(var key in form_structure[mainid]) {                     if(key != 'subfields') {                         form_structure[mainid]['subfields'][key] = get_into_subfields(form_structure[mainid][key]);                         delete(form_structure[mainid][key]);                     }                 }             }             return form_structure;         } 

any ideas doing wrong?

edit:

here jsfiddle

this pretty simplify able, think function complicated. problem not want first 1 subfields object, why not? make easier , same result adding .subviews result.

var obj = {'u1fsqexd1azmnpl' : {'2fndvdkaefad6xq' : {'fz0zn6d51tgvqid' : { '05e1jsfyvhjlgvp' : {}}}}}    function subfields(data){    // create standard object can return    var result = {subfields:{}};    for(var key in data){      // recursively add      result.subfields[key] = subfields(data[key])    }    return result;  }    console.log('the full result', subfields(obj));  console.log('ignore initial subfield returned', subfields(obj).subfields);  document.write('<pre>' + json.stringify(subfields(obj).subfields) + '</pre>')

this leave empty object @ end of chain, assume not problem, although there still lot can make sure won't error out, basics work.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -