如何在Javascript中推送多个数组并从函数返回它们?

时间:2017-09-04 19:25:28

标签: javascript php arrays

在下面的代码片段中,我尝试添加另一个函数,将每行与我知道会出现的行进行比较,这样如果找到它,就会返回一个我需要在屏幕上运行的查询

error: there were errors found during runtime 12.0
warning: incorrect info 1.0
warning: file at 'C://localhost/myfiles' not found 0.0
warning: file at 'C://localhost/css' not found 0.0
warning: file at 'C://localhost/js' not found 0.0
warning: file at 'C://localhost/assets' not found 5615612.0

这是我试图做的,但我不知道如何忽略数字,以便线条可以实际比较。 所有这些都发生在第一部分运行之后,我得到的数字大于0.0。

意味着对这些行进行比较:

错误:在运行时12.0期间发现错误 警告:错误信息1.0
警告:找不到'C:// localhost / assets'的文件5615612.0

这些是我的条件:

if ("error: there were errors found during runtime 12.0" == line){
    //run this query for database runtimeDB
}
if("warning: incorrect info 1.0" == line){
    //this query
    //+1 for incorrectInfoDB
}
if("warning: file at 'C://localhost/myfiles' not found 0.0" == line){
    //this query
    //+1 for incorrectInfoDB
}
if("warning: file at 'C://localhost/css' not found 0.0" == line){
    //this query
    //+1 for outerfilesDB
}
if("warning: file at 'C://localhost/css' not found 0.0" == line){
    //this query
    //+1 for outerfilesDB
}
if("warning: file at 'C://localhost/js' not found 0.0" == line){
    //this query
    //+1 for outerfilesDB
}
if("warning: file at 'C://localhost/assets' not found 0.0" == line){
    //this query
    //+1 for outerfilesDB
}

我在想的是使用getLines()函数,然后转到这个函数,它返回没有最后一个数字的字符串,以便进行比较。

function trim_numbers($string) {
    $numbers = strrchr($string, ' ');
    return str_replace($numbers, '', $string);
}

问题是我不知道如何推送数组以使它们都在同一个getLines()函数中,而且我不必编写更多相同的代码。意思是这个功能

function getLines() {
  var text = $('textarea').val();
  var lines = text.split("\n");
  var requiredLines = [];
  var runtimeDB = [];
  var incorrectInfoDB = [];
  var outerfilesDB = [];
  lines.forEach(function(line) {
    number = line.match(/\d+\.\d+$/);
    if (number != null && number[0] > 0){
      requiredLines.push(line);
      if (trim_numbers(line) == "error: there were errors found during runtime"){
          //run this query for database runtimeDB
          //push this query to be outputed 
      }
      if("warning: incorrect info 1.0" == line){
          //this query for it database array 
          //push this query to be outputed 
      }
    }

  });
  return requiredLines;
}

为每个数据库组添加一个数组

  • runtimeDB
  • incorrectInfoDB
  • outerfilesDB

我不知道我是否正确解释了逻辑 但是

  1. 首先它获得所有高于0.0的值
  2. 然后它将数组从高于0.0的值推送到数据库组
  3. 另外,有没有办法优化我的逻辑?

    function getLines() {
      var text = $('textarea').val();
      var lines = text.split("\n");
      var requiredLines = [];
      lines.forEach(function(line) {
        number = line.match(/\d+\.\d+$/);
        if (number != null && number[0] > 0)
          requiredLines.push(line);
      });
      return requiredLines;
    }
    
    
    $(document).ready(
      function() {
        $('#printlogs').html(getLines().join('<br>'));
      });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <textarea style="width:100%;height:120px">
    error: there were errors found during runtime 12.0
    warning: incorrect info 1.0
    warning: file at 'C://localhost/myfiles' not found 0.0
    warning: file at 'C://localhost/css' not found 0.0
    warning: file at 'C://localhost/js' not found 0.0
    warning: file at 'C://localhost/assets' not found 5615612.0
    </textarea>
    
    <h4>Error Log</h4>
    <printlogs id="printlogs" style="color:red"></printlogs>
    <br>
    <p>after these logs I want to get the arrays of each group that has their own database queries for the ones printed in printlogs</p>
    
    <p>
      ex.
    </p>
    <h4>runtimeDB</h4>
    <p>
      //run this query for this line
    </p>
    <h4>incorrectInfoDB</h4>
    <p>
      //run this query for this line<br> //run this other query for other line ...
    </p>

2 个答案:

答案 0 :(得分:0)

我会使用正则表达式并匹配特定关键字,以后可用于比较。

在这个特定的正则表达式中,我匹配具有关键字的所有内容,并以数字结尾,并且该数字不是“0.0”(这是正则表达式的(?!0\.0)部分。)

正则表达式在循环中执行,因为它每次迭代匹配一行。

function getLines(text) {
  var lines = [];
  var re = /.*?(runtime|info|myfiles|css|assets|js).*?(?!0\.0)\d+\.?\d+/g;
  while (m = re.exec(text)) {
    lines.push(m[1]);
  };

  return lines;
}

$(function() {
    $('#printlogs').html(getLines($('textarea').val()).join('<br>'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea style="width:100%;height:120px">
error: there were errors found during runtime 12.0
warning: incorrect info 1.0
warning: file at 'C://localhost/myfiles' not found 0.0
warning: file at 'C://localhost/css' not found 0.0
warning: file at 'C://localhost/js' not found 0.0
warning: file at 'C://localhost/assets' not found 5615612.0
</textarea>

<h4>Error Log</h4>
<div id="printlogs" style="color:red"></div>

稍后在您的代码中

getLines().forEach(function(line) {
    if (line === 'info') {
        // do your logic
    }
    if (line === 'js'||line ==='css'||line==='assets') {
        // do your logic
    }
});

答案 1 :(得分:0)

var thestr = "warning: file at 'C://localhost/assets' not found 5615612.0";
var voila = thestr.substr(thestr.indexOf("found") + 5);   //+ 6 to remove the leading space
console.log(thestr);
console.log(thestr.indexOf("found"));
console.log(voila);

以下是将字符串拆分为&#34;找到&#34;,但它闻起来有点气味。

我会编写错误并将代码添加到日志行中一个可识别的位置 - 当然,如果您是选择日志中的内容的人。即。

0001::warning: whatever text

然后你可以解析每一行的第一个字符,或者在&#34; ::&#34;上拆分该行,并且知道你遇到了一个&#34; 0001&#34;错误。并在行尾使用类似的易于解析的方法。