javascript - Manipulate JSON tree structure -
i building form builder, allows user have multiple questions, sub questions, , sub questions sub questions, etc.
i have method works extracting data, that, data / json tree
needs in specific format:
u1fsqexd1azmnpl : { subfields : { 2fndvdkaefad6xq : { subfields : { fz0zn6d51tgvqid : { subfields : { 05e1jsfyvhjlgvp : { subfields : {} } } } } } } }
and how data starts out:
u1fsqexd1azmnpl : { 2fndvdkaefad6xq : { fz0zn6d51tgvqid : { 05e1jsfyvhjlgvp : {} } } }
there can multiple items @ each level, such as:
u1fsqexd1azmnpl : { 2fhjsnnchsjowl2 : {}, 2fndvdkaefad6xq : { fz0zn6d51tgvqid : { 05e1jsfyvhjlgvp : {}, 03jshvijsondjla : {} } } }
my method manipulate data subfields
form structure:
function get_into_subfields(form_structure) { for(var mainid in form_structure) { for(var key in form_structure[mainid]) { if(!form_structure[mainid].hasownproperty('subfields')) { form_structure[mainid]['subfields'] = {}; } if(key != 'title' && key != 'placeholder' && key != 'help' && key != 'type') { // || {} key not have 'undefined' value throws error extract_data method form_structure[mainid]['subfields'][key] = get_into_subfields(form_structure[mainid][key]) || {}; // taking out keeps chain going, not put in subfields key delete(form_structure[mainid][key]); } } } }
but, cuts out @ first subfields
level.
u1fsqexd1azmnpl : { subfields : { 2fndvdkaefad6xq : {} } }
to give overview of form can like, here image few notes on see format.
note:
- each question has
main_id
field containing uniqueid
(i forgot changemy id
on screenshotmain id
on first two. sorry.) - each question references first parent using
original_id
data attribute. (the main question) - each question references it's particular parent using
parent_id
data attribute.
what need do: go through format
u1fsqexd1azmnpl : { subfields : { 2fndvdkaefad6xq : {} } }
and put in format
u1fsqexd1azmnpl : { subfields : { 2fndvdkaefad6xq : { subfields : { fz0zn6d51tgvqid : { subfields : { 05e1jsfyvhjlgvp : { subfields : {} } } } } } } }
how need alter get_into_subfields
method?
this answer replaces {key: value}
{key: {subfields: value}}
, recursively sending value
through same algorithm until property levels have been processed.
var data = { "u1fsqexd1azmnpl" : { "2fhjsnnchsjowl2" : {}, "2fndvdkaefad6xq" : { "fz0zn6d51tgvqid" : { "05e1jsfyvhjlgvp" : {}, "03jshvijsondjla" : {} } } } }; // main object-restructuring function function get_into_subfields(form_structure) { (var key in form_structure) { form_structure[key] = {subfields: get_into_subfields(form_structure[key])}; } return form_structure; } // function showing code in action // here on stack overflow , isn't relevant problem function formatobjforhtml(obj) { document.write("<pre>" + json.stringify(obj, null, 2) + "</pre>"); } formatobjforhtml(data); // view data before processing get_into_subfields(data); // process data formatobjforhtml(data); // view data after processing
note property keys of original data in javascript code in answer explicitly placed in quotation marks because of them start numerals. may not relevant final application if receiving data json. however, in proof-of-concept code includes hard-coded data, quotes required.
i have not included code deals "title", "placeholder", "main_id", etc. discuss in question. not clear question whether these important main problem asking about, i.e. re-structuring data object.
note answer mutates original data
object in place rather creating new, modified object. if want original data left unchanged first deep clone of data , run get_into_subfields(clone_of_data)
.
this answer leaves whatever json conversions require. json stringification in answer there make results viewable in stack overflow code snippet viewer.
Comments
Post a Comment