2017-06-09 46 views
0

我剛剛爲我的問題創建了一個基本用例,在那裏我卡住了,我將有searchStr關鍵字數組對象從客戶端,我會檢查是否所有的搜索關鍵字只與行相比匹配results。與下面的代碼,它總是返回空數組results,如何解決這個問題或任何更好的方式來實現這個任務?如果所有關鍵字匹配,如何將值推到數組中?

search.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) 
+0

不寫'searchStr = [「45287」,「standard」]''的任何理由? –

+0

我使用的角度'ngInputTags'在輸入關鍵字時創建數組對象 – hussain

+0

不相關,但不是使用全局變量,而是在搜索函數內部創建'results'並返回它。 –

回答

0

你不是搜尋在你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回覆

這裏就是我想實現我現在已經chnaged對象數組在這種情況下字符串數組實際代碼發送所有結果。

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 = []; 
     }); 
     } 

    } 
+0

我得到這個錯誤,如果我添加'文本'TypeError:無法讀取屬性'toLowerCase'的undefined – hussain

+0

@hussain然後你做了其他錯誤,因爲我沒有。 –

+0

@DaveNewton我在此答案中添加了代碼,以便更好地理解 – hussain

相關問題