【问题标题】:How to push value to array if all keywords match?如果所有关键字都匹配,如何将值推送到数组?
【发布时间】:2017-06-09 14:13:55
【问题描述】:

我刚刚为我遇到的问题创建了一个基本用例,我将从客户端获得 searchStr 关键字数组对象,我将检查所有搜索关键字是否仅与行匹配,而不是将行推送到 results。下面的代码总是返回results的空数组,如何解决这个问题或任何更好的方法来完成这个任务?

搜索.js

var line = "ticketNumber:45287,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer "

var searchStr = [{
    "text": "45287"
}, {
    "text": "standard"
}]

var results = [];

function matchSearchStr(line, searchStrArray) {
    for (var i = 0; i < searchStrArray.length; i++) {
        if (line.toLowerCase().indexOf(searchStrArray[i].toLowerCase()) != -1) {
            results.push({
                filename: "file",
                value: line
            });
           console.log(results);
        }

    }

}

matchSearchStr(line, searchStr)

【问题讨论】:

  • 有什么理由不写searchStr = ["45287", "standard"]
  • 我正在使用 Angular ngInputTags 在您输入关键字时创建数组对象
  • 不相关,但不使用全局变量,而是在搜索函数中创建 results 并返回它。
  • 同样不相关,但使用当前算法会得到重复的结果。
  • 我可以得到适当的解决方案来解决我在过去 2 小时被卡住的问题..

标签: javascript arrays algorithm search


【解决方案1】:

您没有在 searchStrArray 中搜索 TEXT;试试这个:

function matchSearchStr(line, searchStrArray) {
    for (var i = 0; i < searchStrArray.length; i++) {

        if (line.toLowerCase().indexOf(searchStrArray[i].text.toLowerCase()) != -1) {
            results.push({
                filename: "file",
                value: line
            });
           console.log(results);
        }

    }

}

----@aorfevre 回复的所有者编辑

这里是我正在尝试实现的实际代码,现在我已经将对象数组更改为数组字符串,在这种情况下它会发送所有结果。

    async.eachSeries(filesData.logFiles, function (logfile,done) {
            // read file
            readStream = fs.createReadStream('./logs/' + filesData.searchEnv  + '/'+ logfile.filename,'utf8')
             readStream.pipe(split())
              .on('data', function (line) {

              console.log(searchStr);
              for(var i=0; i<searchStr.length; i++)  {
                if (line.toLowerCase().indexOf(searchStr[i].toLowerCase()) != -1)
           var messageDateInfo = line.split('|')[0].replace(/[\[\]']+/g,'');
                messageDateInfo = new Date( messageDateInfo ).getTime();
                searchStartDate = new Date( searchStartDate ).getTime();
                searchEndDate = new Date( searchEndDate ).getTime();
                if( messageDateInfo - searchStartDate > 0 && searchEndDate - messageDateInfo > 0){
                  // console.log("message date is within this time range");
                  results.push({
                    filename:logfile.filename,
                    value:line
                  });
                }
             }
          });
                  done();
        }
        , function (err) {
            if (err) {
                console.log('error', err);
            }
           readStream.on('end',function(){
            callback(results);
           });
           results = [];
        });
      }

    }

【讨论】:

  • 如果我添加 text TypeError: Cannot read property 'toLowerCase' of undefined 会出现此错误
  • @hussain 那你做错了,因为我没有。
  • @DaveNewton 我已经在这个答案中添加了代码以便更好地理解
  • @hussain Ew 不要-如果您有其他上下文,请编辑问题。根据您问题中的代码,这个答案是正确的 - 它运行良好。
猜你喜欢
  • 2011-01-08
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
  • 2016-12-13
  • 1970-01-01
相关资源
最近更新 更多