javascript - Dynamically add array elements to JSON Object -


i'm creating json object array , want dynamically push data json object based on values array. see code better understanding of problem...

for(i=0;i<duplicates.length; i++) {   var request = {     "name": duplicates[i].scope,     "id": 3,     "rules":[       {         "name": duplicates[i].scope + " " + "op sdr sync",         "tags": [           {             "tagid": 1,             "variables":[               {                 "variable": duplicates[i].variable[j],                 "matchtype": "regex",                 "value": duplicates[i].scopedef               }             ],             "condition": false,           },           {             "tagid": 1,             "condition": false,           }         ],         "rulesetid": 3,       }     ]   } } 

i take object properties duplicates array can have following elements:

[{scopedef=.*, scope=global, variable=[trackingcode, v1, v2]}, {scopedef=^https?://([^/:\?]*\.)?delta.com/products, scope=products section, variable=[v3]}]

as can see, object contain variable element can have multiple values. need push json object values dynamically (meaning there more 3 values in array).

for example, after push values duplicates array, json object should this:

name=products section,    rules=   [     {       name=products section op sdr sync,        tags=[       {          variables=          [            {              matchtype=regex,               variable=v3,               value=^https?://([^/:\?]*\.)?delta.com/products            },            {              matchtype=regex,               variable=trackingcode,               value=.*            },            {              matchtype=regex,               variable=v1,               value=.*            },            {              matchtype=regex,               variable=v2,               value=.*            }          ],           condition=false,         },         {          condition=false,           tagid=1        }      ],       rulesetid=3    }   ] } 

i tried following code without success:

  for(var j in duplicates[i].variable) {     var append = json.parse(request);     append['variables'].push({       "variable":duplicates[i].variable[j],       "matchtype": "regex",       "value": duplicates[i].scopedef     })   } 

please let me know if need provide additional information, started working json objects.

first of all, dont need parse request, create object, parse when json string, like:

var json='{"a":"1", "b":"2"}'; var x = json.parse(json); 

next, have property of object wrapped in arrays. correctly work should write:

request.rules[0].tags[0].variables.push({   "variable":duplicates[i].variable[j],   "matchtype": "regex",   "value": duplicates[i].scopedef }) 

if want use code snippet, need changes in request:

  var request = { "name": duplicates[i].scope, "id": 3, "variables":[    {    "variable": duplicates[i].variable[j],    "matchtype": "regex",    "value": duplicates[i].scopedef    }            ], "rules":[   {     "name": duplicates[i].scope + " " + "op sdr sync",     "tags": [       {         "tagid": 1,         "condition": false,       },       {         "tagid": 1,         "condition": false,       }     ],     "rulesetid": 3,   } ] } } 

to understand json remember basic rule: read json backward. means:

  • property
  • object.property
  • arrayofobfects['id'].object.property
  • mainobject.arrayofobfects['id'].object.property

and on. luck!


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -