我想从当前数组中删除孤儿(非匹配对):
J01171 01/11/2012 08:03:34 J01171 Bath Rd Ipswich Reconnected
J01171 01/11/2012 08:04:34 J01171 Bath Rd Ipswich Isolated by OTU Fault
J01171 01/11/2012 08:04:47 J01171 Bath Rd Ipswich Reconnected
J01171 02/01/2013 15:46:22 J01171 Bath Rd Ipswich Isolated by OTU Fault
J01171 02/01/2013 15:46:36 J01171 Bath Rd Ipswich Reconnected
J01171 01/02/2013 18:12:43 J01171 Bath Rd Ipswich Isolated by OTU Fault
J01171 01/02/2013 18:42:32 J01171 Bath Rd Ipswich Reconnected
J01181 10/12/2012 13:13:13 J01181 BathRd LeighRd Isolated by Fault
J01181 10/12/2012 15:30:01 J01181 BathRd LeighRd Reconnected
J01181 09/02/2013 00:43:00 J01181 BathRd LeighRd Isolated by OTU Fault
J01181 09/02/2013 00:47:57 J01181 BathRd LeighRd Reconnected
J01181 09/02/2013 00:49:00 J01181 BathRd LeighRd Isolated by OTU Fault
删除孤儿后,输出应如下所示:
J01171 01/11/2012 08:04:34 J01171 Bath Rd Ipswich Isolated by OTU Fault
J01171 01/11/2012 08:04:47 J01171 Bath Rd Ipswich Reconnected
J01171 02/01/2013 15:46:22 J01171 Bath Rd Ipswich Isolated by OTU Fault
J01171 02/01/2013 15:46:36 J01171 Bath Rd Ipswich Reconnected
J01171 01/02/2013 18:12:43 J01171 Bath Rd Ipswich Isolated by OTU Fault
J01171 01/02/2013 18:42:32 J01171 Bath Rd Ipswich Reconnected
J01181 10/12/2012 13:13:13 J01181 BathRd LeighRd Isolated by Fault
J01181 10/12/2012 15:30:01 J01181 BathRd LeighRd Reconnected
J01181 09/02/2013 00:43:00 J01181 BathRd LeighRd Isolated by OTU Fault
J01181 09/02/2013 00:47:57 J01181 BathRd LeighRd Reconnected
排序数组中的所有元素成对出现在所有资产代码中“隔离 - 重新连接”。但是,该阵列已经获得了一些资产代码的孤儿:在顶部有不匹配的“重新连接”(因为它的“隔离”对已经留在其他日志文件中)而在底部有不匹配的“隔离” (因为它的'Reconnected'对将在未来的日志文件中)。我的任务是摆脱所有孤儿。我在这里只提供了2个资产代码,但实际上有数百个(或者甚至可能是数千个)资产代码,数组中有数百万个元素,因此有数百个孤儿。
孤儿也可能出现在资产代码的中间。基本上,可能存在三个“隔离”将在任何给定资产代码的中间彼此跟随的情况。我需要在第一个'隔离'之后删除'隔离'后面,因为它还没有得到它的对。例如,
X00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Isolated
X00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Isolated [NEEDS TO BE REMOVED]
X00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Isolated [NEEDS TO BE REMOVED]
X00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Reconnected
J00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Isolated
J00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Isolated [NEEDS TO BE REMOVED]
J00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Reconnected
J00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Reconnected [NEEDS TO BE REMOVED]
有什么想法来处理这个问题吗?提前谢谢。
答案 0 :(得分:1)
此fiddle似乎按预期工作:
var orph_arr = [
"J01171 01/11/2012 08:03:34 J01171 Bath Rd Ipswich Reconnected",
"J01179 01/11/2012 08:04:34 J01179 Bath Rd Ipswich Isolated by OTU Fault", // Note: this guy is an orphan according to the OP's definition
"J01171 01/11/2012 08:04:34 J01171 Bath Rd Ipswich Isolated by OTU Fault",
"J01171 01/11/2012 08:04:47 J01171 Bath Rd Ipswich Reconnected",
"J01171 02/01/2013 15:46:22 J01171 Bath Rd Ipswich Isolated by OTU Fault",
"J01171 02/01/2013 15:46:36 J01171 Bath Rd Ipswich Reconnected",
"J01171 01/02/2013 18:12:43 J01171 Bath Rd Ipswich Isolated by OTU Fault",
"J01171 01/02/2013 18:42:32 J01171 Bath Rd Ipswich Reconnected",
"J01181 10/12/2012 13:13:13 J01181 BathRd LeighRd Isolated by Fault",
"J01181 10/12/2012 15:30:01 J01181 BathRd LeighRd Reconnected",
"J01181 09/02/2013 00:43:00 J01181 BathRd LeighRd Isolated by OTU Fault",
"J01181 09/02/2013 00:47:57 J01181 BathRd LeighRd Reconnected",
"J01181 09/02/2013 00:49:00 J01181 BathRd LeighRd Isolated by OTU Fault"];
for (var i = 0; i < orph_arr.length - 1; i++) {
var asset1 = orph_arr[i].match(/[A-Z]{1}\d{5}/);
var asset2 = orph_arr[i + 1].match(/[A-Z]{1}\d{5}/);
var isolated1 = orph_arr[i].match(/\b(Isolated)\b/gi);
var isolated2 = orph_arr[i + 1].match(/\b(Isolated)\b/gi);
var reconnected1 = orph_arr[i].match(/\b(Reconnected)\b/gi);
var reconnected2 = orph_arr[i + 1].match(/\b(Reconnected)\b/gi);
if ((asset1[0] !== asset2[0]) && (reconnected1) && (reconnected2)) {
orph_arr[i + 1] = "REMOVED";
}
if ((asset1[0] !== asset2[0]) && (isolated1) && (isolated2)) {
orph_arr[i] = "REMOVED";
}
}
console.dir(orph_arr);
我通过替换孤儿而不是删除它来简化它,但显然它们可以被删除(一个简单的orph_arr.splice(i,1);
应该删除它),虽然我认为如果你不删除它们通常会更好在迭代它时列表。它往往会搞砸你的索引。
这是一个实际删除而不是替换的fiddle。注意如何重新设置数组索引,或者你会错过两个连续的孤儿。
答案 1 :(得分:1)
我认为这可以满足您的需求:
isolated = {}
result = data.reduce(function(buf, line) {
var m = line.match(/(^[A-Z]\d{5}).*?(Reconnected|Isolated)/);
var asset = m[1], event = m[2];
if(event == "Reconnected" && asset in isolated) {
buf[isolated[asset]] = buf[isolated[asset]].substr(1);
delete isolated[asset];
buf.push(line);
} else if(event == "Isolated") {
isolated[asset] = buf.push("?" + line) - 1;
}
return buf;
}, []). filter(function(line) {
return line.charAt(0) != "?";
})
这个想法是跟踪“隔离”线并在找到匹配的“重新连接”后“恢复”清理。请注意,此代码不需要“隔离”后立即“重新连接”,并且可以处理来自不同资产的混合输出的日志。