Reputation: 4610
I have an array of strings and each string contains a key word plus a number (the number value is different, only the key word is constant) and I want to know how can I get the word and also the number...see the example below:
var keyWords = ["data","action","type"];
var stringArray = ["set data4 to custom value '1'",
"set data110 to custom value '2'",
"overwrite value of action1023 with new value",
"set type23 to custom value '10'",
"overwrite value of action13 with text",
"set type48 to custom value '7'"]
And the response should be like this:
var response = ["data4","data110","action13","type23","action1023","type48"];
This is what I've managed to do:
var response = [];
for(var j=0; j<keyWords.length; j++) {
for(var i=0; i<stringArray.length; i++) {
if(stringArray[i].indexOf(keyWords[j]) !== -1) {
response.push(keyWords[j]);
}
}
}
But it only returns me the exact key words, without numbers..
response = [data, data, action, action, type, type]
Upvotes: 0
Views: 320
Reputation: 13366
... a more generic and functional approach ...
var
keyList = ["data", "action", "value", "type"],
sentenceList = [
"set data4 to custom value '1'",
"set data110 to custom value '2'",
"overwrite value of action1023 with new value",
"set type23 to custom value '10'",
"overwrite value of action13 with text",
"set type48 to custom value '7'"
],
wordList = sentenceList.reduce(function collectSpecificWords (collector, sentence) {
var
matchList = sentence.split(/\s+/).filter(function (word) {
return collector.regXWord.test(word);
});
collector.wordList = collector.wordList.concat(matchList);
return collector;
}, {
//regXWord: RegExp("^("+ keyList.join("|") + ")[0-9]+$"),
regXWord: RegExp("^("+ keyList.join("|") + ")[0-9]*$"),
wordList: []
}).wordList;
console.log("wordList : ", wordList);
Upvotes: 0
Reputation: 770
If you would like to take the regex approach,
this example might be what you were looking for:
var found = [],
result;
for(var i in stringArray){
result = stringArray[i].match(/(data|action|value|type)(\d+)/);
found.push(result);
}
in the "found" array you should get a list of items, each with the following data:
An example output:
[["data4", "data", "4"], ["data110", "data", "110"], ["action1023", "action", "1023"], ["type23", "type", "23"], ["action13", "action", "13"], ["type48", "type", "48"]]
Hope it helps a bit.
Upvotes: 2
Reputation: 1771
Very straightforward approach:
var keyWords = ["data","action","value","type"];
var stringArray = ["set data4 to custom value '1'",
"set data110 to custom value '2'",
"overwrite value of action1023 with new value",
"set type23 to custom value '10'",
"overwrite value of action13 with text",
"set type48 to custom value '7'"]
function f(keyWords, stringArray){
let output = []
stringArray.forEach((sentence) => {
var words = sentence.split(' ')
words.forEach((word) => {
keyWords.forEach((keyword) => {
if(word.indexOf(keyword) > -1 ) {
output.push(word)
}
})
})
})
return output
}
console.log(f(keyWords, stringArray))
Upvotes: 0
Reputation: 96
This code does the job
var keyWords = ["data","action","value","type"];
var stringArray = ["set data4 to custom value '1'",
"set data110 to custom value '2'",
"overwrite value of action1023 with new value",
"set type23 to custom value '10'",
"overwrite value of action13 with text",
"set type48 to custom value '7'"]
var response = [];
for(var i = 0; i < stringArray.length; ++i){
for(var j = 0; j < keyWords.length; ++j){
if((index = stringArray[i].indexOf(keyWords[j])) !== -1){
splittedStr = stringArray[i].substring(index, stringArray[i].length).split(' ', 1);
response.push(splittedStr[0]);
}
}
}
console.log(response);
Upvotes: 1