简体   繁体   English

搜索/过滤从文本文件创建的 Javascript 数组

[英]Search/Filter Javascript Array which was created from a text file

I hope someone have an idea or could give me some input on my problem.我希望有人有想法或可以就我的问题给我一些意见。

Starting scenario: A .txt Logfile is created by a software and I try to create a website query which shows contained error messages from that log clearly arranged among each other.起始场景:一个 .txt 日志文件是由软件创建的,我尝试创建一个网站查询,该查询显示该日志中包含的错误消息清楚地排列在彼此之间。

So far I have the code to read the file as a variable, map the content as an array and split it to single lines.到目前为止,我有代码将文件作为变量读取,将内容映射为数组并将其拆分为单行。

<html>
<div>
Select Errorlog:
<input type="file" id="fileInput">
</div>

<pre id="displayFile"></pre>
<script>

   var fileInput = document.getElementById('fileInput');

   fileInput.addEventListener('change', function(e) {

          var file = fileInput.files[0];
          var reader = new FileReader();

   reader.onload = function(e) {

          var dateiinhalt = reader.result;
          var arr = dateiinhalt.split("\n");

   document.getElementById("Errorlog").innerHTML = arr[1];

};
 reader.readAsText(file);           
});
</script>
<p id="Errorlog"></p>
</html>

That was the easy job, but now I'm desperate to filter the array or to display certain index values of the array.那是一项简单的工作,但现在我迫切希望过滤数组或显示数组的某些索引值。

The Logfile itself looks like日志文件本身看起来像

2020-02-26 00:00:00 | ERROR | Message
2020-02-26 00:00:01 | INFO | NewEvent
2020-02-26 00:00:01 | ERROR | Message 2

The goal is to show every line which contains "ERROR" at once.目标是一次显示包含“ERROR”的每一行。 I tried several filter/search functions, but everything I tried did not work (fe array.prototype.filter or indexOf).我尝试了几个过滤/搜索功能,但我尝试的所有功能都不起作用(fe array.prototype.filter 或 indexOf)。

Assuming that after the .split() you have an array of log lines as strings, a simple way to filter it for errors would be:假设在.split()你有一个日志行数组作为字符串,一个简单的过滤错误的方法是:

var arr = dateiinhalt.split("\n");
arr = arr.filter(line => line.indexOf("ERROR") >= 0);

or, in a more old-fashioned style:或者,以更老式的风格:

arr = arr.filter(function(line) { return line.indexOf("ERROR") >= 0; });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM