【问题标题】:Function only returning first value函数只返回第一个值
【发布时间】:2020-11-20 21:18:27
【问题描述】:

我有一个函数可以解析消息体并将其拆分为单独的属性。但是,当我运行它时,我只会返回第一个值。关于我所缺少的任何建议,请注意必须是 ES5。

预期结果:

{Current Update: "Investgations in progress.",
      Description: "Service Portal Unavailable",
      Impact: "Customers unable to access Service Portal",
      Incident number: "INC0012345",
      Incident start date/time: "27/07/2020 12",
      Ref: "MSG3207258_sCxJ4T6p2y21HH2w4xdS",
      Services affected: "Service 1"}

实际结果:

{Incident number: "INC0012345"}

输入 msgBody:

{"Incident number":" INC0012345\\n\\nIncident start date/time: 27/07/2020 12:56:59 AEST\\n\\nServices affected: Service 1\\n\\nDescription: Service Portal Unavailable\\n\\nImpact: Customers unable to access Service Portal\\n\\nCurrent Update: Investgations in progress.\\n\\nRef:MSG3207258_sCxJ4T6p2y21HH2w4xdS"}

脚本:

(function execute(inputs, outputs) {
  var props = inputs.msgBody.split(/\\n+/);
  var res = {}; // =[];

  for (i = 0; i < props.length; i++) {
    var tmp = props[i].match(/(.*?):(.+)/);
    if (tmp) {
      if (tmp[1] && tmp[2]) {
        res[tmp[1]] = tmp[2].trim();
      }
    }
  }

  outputs.payload = JSON.stringify(res);
})(inputs, outputs);

【问题讨论】:

    标签: javascript ecmascript-5


    【解决方案1】:

    这是我的方法,我认为它与 es5 兼容,与我刚刚添加的代码没有太大区别filter

    inputs = {
      "Incident number":
        " INC0012345\\n\\nIncident start date/time: 27/07/2020 12:56:59 AEST\\n\\nServices affected: Service 1\\n\\nDescription: Service Portal Unavailable\\n\\nImpact: Customers unable to access Service Portal\\n\\nCurrent Update: Investgations in progress.\\n\\nRef:MSG3207258_sCxJ4T6p2y21HH2w4xdS",
    };
    
    function execute(inputs) {
      var props = ("Incident number :" + inputs["Incident number"]).split(/(\\n)/g);
      var prop = props.filter((n) => n!='\\n' && n!="");
      var res = {};
      for (i = 0; i < prop.length; i++) {
    var tmp = prop[i].split(/\s?:(?=[^0-9])\s?/);
        {
          res[tmp[0]] = tmp[1];
        }
      }
      return res;
    }
    
    console.log(execute(inputs));

    【讨论】:

      【解决方案2】:

      我承认这是我第一次尝试编写与 ES5 兼容的代码。它采用与您不同的方法。但我相信它会产生预期的结果。

      var raw = { "Incident number": " INC0012345\\n\\nIncident start date/time: 27/07/2020 12:56:59 AEST\\n\\nServices affected: Service 1\\n\\nDescription: Service Portal Unavailable\\n\\nImpact: Customers unable to access Service Portal\\n\\nCurrent Update: Investgations in progress.\\n\\nRef:MSG3207258_sCxJ4T6p2y21HH2w4xdS" };
      var medium = "Incident number:" + raw["Incident number"];
      var done = medium.split("\\n\\n");
      var wellDone = done.map(function (elem) {
        var key = elem.split(":")[0];
        var colonIndex = elem.indexOf(":");
        var value = elem.slice(colonIndex + 1).replace(/ /, '');
        return new Array(key, value);
      });
      var served = new Object();
      
      for (var i = 0; i < wellDone.length; i++) {
        served[wellDone[i][0]] = wellDone[i][1];
      }
      
      console.log(served);

      【讨论】:

        猜你喜欢
        • 2011-11-05
        • 2019-03-22
        • 1970-01-01
        • 2014-01-03
        • 2021-08-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多