Using Node.js v12.18.3
mongodb3.6 installed
apps\25.8.0\e\366f.QkC0\distr\lib\custom\ivcxlkisls.js
I just think his example is too old dated
@sergerdn said in Check if variable contains something from a list:
Give me an example of the values you are looking for, and I will provide some JavaScript code for you.
That would be amazing. ebub, ebook, audiobook for example, simple keywords :)
Thanks
Main / Tools / Execute code
Not tested very much.
function findSentencesWithKeyword(keyword, sentences) {
return sentences.filter(function (sentence) {
var words = sentence.toLowerCase().split(" ");
var found = false;
words.forEach(function (word) {
if (word === keyword.toLowerCase()) {
found = true;
}
});
return found;
});
}
log("running ... ");
var sentences = [
"I love reading ebooks on my tablet.",
"Do you have an audiobook recommendation?",
"She prefers printed books to ebooks.",
"I can't find my ebub anywhere.",
"Where can I buy an audiobook online?"
];
var keywords = ["ebub", "ebook", "audiobook"];
keywords.forEach(function (keyword) {
var matchingSentences = findSentencesWithKeyword(keyword, sentences);
log("Keyword: " + keyword);
log(matchingSentences);
log("---------------");
});
Result:
[704642835] [02:19:41] Thread #1 : running ...
[704642835] [02:19:41] Thread #1 : Keyword: ebub
[704642835] [02:19:41] Thread #1 : I can't find my ebub anywhere.
[704642835] [02:19:41] Thread #1 : ---------------
[704642835] [02:19:41] Thread #1 : Keyword: ebook
[704642835] [02:19:41] Thread #1 :
[704642835] [02:19:41] Thread #1 : ---------------
[704642835] [02:19:41] Thread #1 : Keyword: audiobook
[704642835] [02:19:41] Thread #1 : Do you have an audiobook recommendation?,Where can I buy an audiobook online?
[704642835] [02:19:41] Thread #1 : ---------------
@sergerdn
Thank a lot!
Its not excatcly what I looked for but based on your code I made a few changes and have a working solution now:
function findKeywordsInTitle(keywords, title) {
var words = title.toLowerCase().split(/\W+/);
var foundKeywords = [];
keywords.forEach(function (keyword) {
if (words.indexOf(keyword.toLowerCase()) !== -1) {
foundKeywords.push(keyword);
}
});
return foundKeywords;
}
var title = [[VARIABLE]];
var keywords = [[KEYWORDS]];
var keywordsPresent = findKeywordsInTitle(keywords, title);
[[KEYWORD_PRESENT_LIST]] = keywordsPresent.join(', ');
[[KEYWORD_PRESENT]] = keywordsPresent.length > 0; //true or false
if ([[KEYWORD_PRESENT]]) {
log("Keywords found - " + [[KEYWORD_PRESENT_LIST]]);
}