使用数组推送获得额外的括号

时间:2014-11-05 21:40:55

标签: javascript node.js

我正在尝试构建一个小型自定义记录器,我注意到我的阵列推送,我得到额外的括号,无法弄清楚发生了什么。这是示例输出

{
"GuestCheckout": [
    {
        "log": "Product Id: 78-1212121",
        "screenshot": ""
    },
    [
        {
            "log": "Product Color: undefined Product Size: 6 Lb Qty: 2",
            "screenshot": ""
        }
    ],
    [
        {
            "log": "",
            "screenshot": "results/screenshots/Guest Checkout - Failed.jpg"
        }
    ]
]
}

这是具有记录器功能的类

var logs = {},
curLogs = [];

module.exports = {
    logger: function(log, screenshot) {
      isScreenshot = screenshot || "false";
      curTestName = testName.replace(/ /g,'');
      curLogs = [];

      if (isScreenshot == "true") {
        curLogs.push({
            "log": "",
            "screenshot": log
        });
      } else {
        curLogs.push({
            "log": log,
            "screenshot": ""
        });
      }

      if (curTestName in logs) {
        logs[curTestName].push(curLogs);
      } else {
        logs[curTestName] = curLogs;
      }

      console.log(curLogs);
    }
}

您会注意到第一个日志和屏幕截图,它包裹着{},但第二个实例包含在[]中。此记录器功能被多次调用。我认为这与推动这种方式有关,但我可能是错的。感谢

更新:使用一个解决方案,输出现在看起来像这样

{
  "GuestCheckout": [
    [
        {
            "log": "Product Id: 36-5173230",
            "screenshot": ""
        }
    ],
    [
        {
            "log": "Product Color: undefined Product Size: 6 Lb Qty: 2",
            "screenshot": ""
        }
    ],
    [
        {
            "log": "",
            "screenshot": "results/screenshots/Guest Checkout - Failed.jpg"
        }
    ]
  ]
}

问题是现在所有{logs,screenshots}都包含在括号中。

2 个答案:

答案 0 :(得分:2)

当您将某些内容记录到已存在的测试时,问题就会开始。如果日志已存在,则表示您正在推送数组而不是对象。要解决此问题,您有两种选择。拉出现有的数组进行测试并直接推送到它,或者之后将每个新的日志条目推送到测试中。

var logs = {},
curLogs = [];

module.exports = {
    logger: function(log, screenshot) {
      isScreenshot = screenshot || "false";
      curTestName = testName.replace(/ /g,'');
      if (!logs[curTestName]) {
        // initialize logs entry
        logs[curTestName] = [];
      }
      // get logs entry
      curLogs = logs[curTestName];

      if (isScreenshot == "true") {
        curLogs.push({
            "log": "",
            "screenshot": log
        });
      } else {
        curLogs.push({
            "log": log,
            "screenshot": ""
        });
      }
      console.log(curLogs);
    }
}

var logs = {},
curLogs = [];

module.exports = {
    logger: function(log, screenshot) {
      isScreenshot = screenshot || "false";
      curTestName = testName.replace(/ /g,'');
      curLogs = [];

      if (isScreenshot == "true") {
        curLogs.push({
            "log": "",
            "screenshot": log
        });
      } else {
        curLogs.push({
            "log": log,
            "screenshot": ""
        });
      }

      if (curTestName in logs) {
        Array.push.apply(logs[curTestName], curLogs);
      } else {
        logs[curTestName] = curLogs;
      }

      console.log(curLogs);
    }
}

答案 1 :(得分:1)

您将carlog定义为数组。在第一步中,您将对象推送到数组。这就是没有[]的原因。在第2步和第3步中,您将数组推送到carlog。这就是[]的原因。