如果字符串上的任何单词与另一个字符串匹配,Google Apps 脚本将获取索引?

发布于 2025-01-09 00:34:50 字数 480 浏览 1 评论 0原文

我有一个从工作表范围中获得的一长串角色,存储为数组中的字符串,举个例子,该数组看起来像这样:

arr1 = ["football manager","hockey coach", "fb player","fb coach","footballer"];

我有另一个数组,其中有一个小标签列表,

arr2 = ["football","fb", "footballer","hockey","rugby"];

我试图匹配第一个数组的角色相对于第二个数组的标签。

我一直试图通过循环并获取匹配行的索引来做到这一点:

for(let i in arr1){
arr2.findIndex(s => s.indexOf(arr1[i]) >= 0);
}

但这仅适用于“足球运动员”,因为它是精确匹配,我也需要对所有部分匹配进行分类。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎言笑 2025-01-16 00:34:51

我怀疑某些文本可能有多个标签(arr1)。这是获取每个文本的标签(索引)数组的解决方案:

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);

它将为您提供对象 text_and_tags,如下所示:

{
  'football manager': [ 0 ],
  'fb player': [ 1 ],
  'fb coach': [ 1, 5 ],
  'footballer': [ 2 ],
  'hockey coach': [ 3, 5 ]
}

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 ]
}
焚却相思 2025-01-16 00:34:50

使用以下函数查找与 arr1 中的值匹配的标签索引(来自 arr2 数组)。

详细解释请关注代码注释。

function matchTagIndexes()
{
  // TODO replace with your values
  arr1 = ["football manager","hockey coach", "fb player","fb coach","footballer"];

  // TODO replace with your tags
  arr2 = ["football","fb", "footballer","hockey","rugby"];

  // for all tags create regex objects
  // regex searches for any match that have `tag` surrounded with word (\b) boundaries 
  // see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet#boundary-type_assertions
  const arr2Regexes = arr2.map(tag => new RegExp(`\\b${tag}\\b`, 'i'));

  // loop arr1 values as val
  arr1.map(val => 
    // for each arr2 regex match val
    arr2Regexes.forEach((regex, i) => 
      // if it is matched, log value from arr1 array, matched tag name and tag's index in arr2 array
      val.match(regex) && console.log(`"${val}" matches tag "${arr2[i]}" which has index ${i}`)
    )
  );
}

结果:

时间状态消息
8:46:35 PM通知执行开始
8:46:35 PM信息“football manager”与索引为 0 的标签“football”匹配
8:46:35 PM信息“hockey coach”与标签“hockey”匹配其中索引为 3
8:46:35 PM信息“fb player”匹配标签“fb”,索引为 1
8:46:35 PM信息“fb coach”匹配标签索引为 1
8:46:35 PM的“fb”信息“footballer”与索引为 2
8:46:36 PM 的标签“footballer”匹配通知执行已完成

参考:

Use following function to find indexes of tags (from arr2 array) that match values from arr1.

Follow code comments for detailed explanation.

function matchTagIndexes()
{
  // TODO replace with your values
  arr1 = ["football manager","hockey coach", "fb player","fb coach","footballer"];

  // TODO replace with your tags
  arr2 = ["football","fb", "footballer","hockey","rugby"];

  // for all tags create regex objects
  // regex searches for any match that have `tag` surrounded with word (\b) boundaries 
  // see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet#boundary-type_assertions
  const arr2Regexes = arr2.map(tag => new RegExp(`\\b${tag}\\b`, 'i'));

  // loop arr1 values as val
  arr1.map(val => 
    // for each arr2 regex match val
    arr2Regexes.forEach((regex, i) => 
      // if it is matched, log value from arr1 array, matched tag name and tag's index in arr2 array
      val.match(regex) && console.log(`"${val}" matches tag "${arr2[i]}" which has index ${i}`)
    )
  );
}

Result:

timestatusmessage
8:46:35 PMNoticeExecution started
8:46:35 PMInfo"football manager" matches tag "football" which has index 0
8:46:35 PMInfo"hockey coach" matches tag "hockey" which has index 3
8:46:35 PMInfo"fb player" matches tag "fb" which has index 1
8:46:35 PMInfo"fb coach" matches tag "fb" which has index 1
8:46:35 PMInfo"footballer" matches tag "footballer" which has index 2
8:46:36 PMNoticeExecution completed

Reference:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文