'Google Apps Script to obtain the index if any of the words on a string match another string?
I have a long list of roles obtained from a sheet range stored as strings in an array, to give an example the array looks something like this:
arr1 = ["football manager","hockey coach", "fb player","fb coach","footballer"];
and I have another array in which I have a small list of tags
arr2 = ["football","fb", "footballer","hockey","rugby"];
I am trying to match the roles of the first array to the tags on the second one.
I have been trying to do this by looping through and obtaining the index of the matched row:
for(let i in arr1){
arr2.findIndex(s => s.indexOf(arr1[i]) >= 0);
}
But this only works for "footballer" as it is an exact match, I need for all of the partial matches to be classified as well.
Solution 1:[1]
I suspect there could be several tags for some of the texts (arr1). Here is the solution to get the array of tags (indexes) for every of the texts:
var texts = ['football manager','hockey coach', 'fb player','fb coach','footballer', 'none'];
var tags = ['football','fb', 'footballer','hockey','rugby', 'coach'];
// get all tags for all the texts
var list = [];
for (let tag of tags) {
var mask = RegExp('\\b' + tag + '\\b', 'i');
for (let text of texts) {
if (text.match(mask))
list.push( {'text': text, 'tag': tag, 'tag_index': tags.indexOf(tag)} );
}
}
console.log(list);
// group tags for the same texts
var text_and_tags = {};
for (let element of list) {
try { text_and_tags[element.text].push(element.tag_index) }
catch(e) { text_and_tags[element.text] = [element.tag_index] }
}
console.log(text_and_tags);
It will get you the object text_and_tags as follows:
{
'football manager': [ 0 ],
'fb player': [ 1 ],
'fb coach': [ 1, 5 ],
'footballer': [ 2 ],
'hockey coach': [ 3, 5 ]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
