【发布时间】: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);
【问题讨论】: