动态将数组元素添加到JSON对象 [英] Dynamically add array elements to JSON Object

查看:118
本文介绍了动态将数组元素添加到JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数组创建JSON对象,并且我想根据数组中的值将数据动态推送到此JSON对象.请参阅我的代码以更好地了解我的问题...

I'm creating a JSON object from an array and I want to dynamically push data to this JSON object based on the values from array. See my code for a better understanding of my 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,
      }
    ]
  }
}

我从duplicates数组中获取对象属性,该对象属性可以包含以下元素:

I take object properties from the duplicates array that can have the following elements:

[{scopeDef=.*, scope=Global, variable=[trackingcode, v1, v2]}, {scopeDef=^https?://([^/:\?]*\.)?delta.com/products, scope=Products Section, variable=[v3]}]

如您所见,对象包含variable元素,该元素可以具有多个值.我需要将所有这些值动态推入JSON对象(这意味着数组中可能有3个以上的值).

As you can see, an object contain variable element that can have multiple values. I need to push to the JSON object all those values dynamically (meaning that there could be more than 3 values in an array).

例如,当我从duplicates数组中推送所有值之后,我的JSON对象应如下所示:

For example, after I push all the values from the duplicates array, my JSON object should look like 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 the following code but 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
    })
  }

如果需要提供其他信息,请告诉我,我刚刚开始使用JSON对象.

Please let me know if I need to provide additional information, I just started working with JSON objects.

推荐答案

首先,您不需要解析请求,您已经创建了一个对象,仅当您将JSON作为字符串获取时才进行解析,例如:

First of all, you dont need to parse request, you already create an object, parse only when you get JSON as string, like:

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

接下来,您具有包装在数组中的对象的任何属性.要正确使用它,您应该编写:

Next, you have any property of object wrapped in arrays. To correctly work with it you should write:

request.rules[0].tags[0].variables.push({
  "variable":duplicates[i].variable[j],
  "matchType": "Regex",
  "value": duplicates[i].scopeDef
})

如果要使用代码段,则需要对请求进行一些更改:

If you want to use your code snippet, you need some 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,
  }
]
}
}

要了解JSON,请记住基本规则:向后读取JSON.这意味着:

To understand JSON remember basic rule: read JSON backward. It means:

  • 财产
  • object.property
  • arrayOfObfects ['id'].object.property
  • mainObject.arrayOfObfects ['id'].object.property

,依此类推.祝你好运!

and so on. Good luck!

这篇关于动态将数组元素添加到JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆