我希望迭代并将文本键中的文本打印到控制台。例如,如果这与字符串"foo bar“匹配,我希望将"foo bar”打印到控制台。
var stringSearcher = require('string-search');
stringSearcher.find('This is the string to search text in', 'string' .then(function(resultArr) {
//resultArr => [ {line: 1, text: 'This is the string to search text in'} ]
});`
发布于 2018-09-28 22:37:33
在普通的nodejs中,我会这样做:
var source = "Hello world";
var target = "Hello";
source_arr = source.split(" ");
source_arr.forEach(function(word){
if(word.trim() === target){
console.log("target");
}
})发布于 2018-09-28 23:25:40
如果您想要得到的只是所显示的resultArr的text属性,那么您可以这样做:
console.log(resultArr[0].text)在实际代码中,您可能希望验证数组是否具有.length > 0,如果有多个结果,则可能希望显示所有匹配的结果。
要迭代所有匹配结果:
const stringSearcher = require('string-search');
stringSearcher.find('This is the string to search text in', 'string'.then(function(resultArr) {
for (let obj of resultArr) {
console.log(obj.text);
}
});来解释。resultArr是一个对象数组。因此,当您迭代数组时,在数组中的每个点都会得到一个对象。然后,要从每个对象获取text属性,可以使用obj.text。
https://stackoverflow.com/questions/52557478
复制相似问题