空元素被添加到Object [英] Empty element is added to Object

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

问题描述

我从 duplicates 数组创建了一个对象,并动态推送到它的元素。一切正常,除了我得到一个额外的空元素,我无法弄清楚为什么...

I create an object from duplicates array and I dynamically push to it elements. Everything works fine, excepting that I'm getting an additional empty element and I can't figure it out why...

这是我的代码:

for(i=0;i<duplicates.length; i++) {
  Logger.log(duplicates[i]);
  var request = {
    "name": duplicates[i].scope,
    "id": 3,
    "rules":[
      {
        "name": duplicates[i].scope + " " + "OP SDR Sync",
        "tags": [
          {
            "tagId": 1,
            "variables":[
              {
              }
            ],
            "condition": false,
          },
          {
            "tagId": 1,
            "condition": false,
          }
        ],
        "ruleSetId": 3,
      }
    ]
  }

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

以下是一个例子:

重复 = [
{scopeDef =。*,scope = Global 4,variable = [trackingcode] ,v1,v2]},{scopeDef = https://www.delta.com/,cope = Homepage 2,variable = [v4,v5,v6,v7]},
]

执行代码后,我得到以下日志:

After I execute the code I get the following log:

第一个对象

{name=Global 4, rules=[{name=Global 4 OP SDR Sync, tags=[{variables=[
{}, 
{matchType=Regex, variable=trackingcode, value=.*}, 
{matchType=Regex, variable=v1, value=.*}, 
{matchType=Regex, variable=v2, value=.*}], condition=false, tagId=1.0}, {condition=false, tagId=1.0}], ruleSetId=3.0}], id=3.0}

第二个对象

name=Homepage 2, rules=[{name=Homepage 2 OP SDR Sync, tags=[{variables=[
{}, 
{matchType=Regex, variable=v4, value=https://www.delta.com/}, 
{matchType=Regex, variable=v5, value=https://www.delta.com/}, 
{matchType=Regex, variable=v6, value=https://www.delta.com/}, 
{matchType=Regex, variable=v7, value=https://www.delta.com/}], condition=false, tagId=1.0}, {condition=false, tagId=1.0}], ruleSetId=3.0}], id=3.0}

请注意,两个对象都包含一个空元素...为什么要添加该元素,如何摆脱它??

Note that both objects contain an empty element...why is that element added and how can I get rid of it??

推荐答案

你在这里定义了这个空对象:

You defined that empty object here:

"variables":[
              {
              }
            ],

这篇关于空元素被添加到Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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