Javascript - Adding objects to an object - how to handle arrays? -


working javascript object, struggling figure out how add new data (specifically more objects) it. when comes adding data, not sure how turn existing value array of objects.

here example of object:

it's pretty straight forward, doesn't contain arrays @ point. stuck.

var obj = {   "data": {     "versionfortarget": "1",     "rulescontainer": {       "rule": { // <--- new rule goes here         "ruleparentid": "1",         "ruleversionid": "1",         "mappedvalue": "1",         "processingorder": "1",         "metainsertutc": "2017-03-03t17:54:34.643",         "value": "omaha",         "isruleretired": "0",         "userimpactcount": "2277",         "attributecontainer": {           "attribute": { // <--- new attribute goes here             "attributeid": "6",             "attributename": "campus",             "attributedetailscontainer": {               "attributedetails": { // <--- new attribute details goes here                 "ruledetailid": "1",                 "attributeid": "6",                 "operatorid": "3",                 "attributevalue": "1",                 "ruleparentid": "1",                 "value": "omaha",                 "isvalueretired": "0",                 "operatorname": "in list",                 "sqloperator": "in"               }             }           }         }       }     }   } }; 

if wanted add new object of data rule, rule becomes array of rule objects.

what going for:

i trying make 3 functions. 1 add rule, 1 add attribute specified rule, , 1 adds attributedetails specified attribute in specified rule.

// add new rule function addrule() {    var newrule = {     "ruleparentid": "3",     "ruleversionid": "1",     "mappedvalue": "4",     "processingorder": "5",     "metainsertutc": "2017-03-03t17:54:34.643",     "value": "new thing",     "isruleretired": "0",     "userimpactcount": "2277"   };  }   // add new attribute function addattribute(ruleparentid) {    // add attribute object in our parent rule    var newattribute = {     "attributeid": "6",     "attributename": "campus",   }  }   // add new attribute details function addattributedetails(ruleparentid, attributeid) {    // add attribute details our specified attribute in our specified parent rule    var newattributedetails = {     "ruledetailid": "1",     "attributeid": "6",     "operatorid": "3",     "attributevalue": "1",     "ruleparentid": "1",     "value": "omaha",     "isvalueretired": "0",     "operatorname": "in list",     "sqloperator": "in"   }  } 

my question

what approaches can take take this? thought looping through data rulecontainer example rule either may or may not array.

i have read through of documentation lodash , have used of functions _find @ least bearings on need manipulate can't figure out go there.

any suggestions?

here fiddle of setup example of outcome possible such working functions.

https://jsfiddle.net/tor1t2q6/1/

to solve problem have rulecontainer array instead of object.

var obj = {   "data": {     "versionfortarget": "1",     "rulescontainer": [//array bracket       "rule": { // <--- new rule goes here         "ruleparentid": "1",         "ruleversionid": "1",         "mappedvalue": "1",         "processingorder": "1",         "metainsertutc": "2017-03-03t17:54:34.643",         "value": "omaha",         "isruleretired": "0",         "userimpactcount": "2277",         "attributecontainer": {           "attribute": { // <--- new attribute goes here             "attributeid": "6",             "attributename": "campus",             "attributedetailscontainer": {               "attributedetails": { // <--- new attribute details goes here                 "ruledetailid": "1",                 "attributeid": "6",                 "operatorid": "3",                 "attributevalue": "1",                 "ruleparentid": "1",                 "value": "omaha",                 "isvalueretired": "0",                 "operatorname": "in list",                 "sqloperator": "in"               }             }           }         }       }     ]   } }; 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -