如果字符串上的任何单词与另一个字符串匹配,Google Apps 脚本将获取索引?
我有一个从工作表范围中获得的一长串角色,存储为数组中的字符串,举个例子,该数组看起来像这样:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑某些文本可能有多个标签(
arr1
)。这是获取每个文本的标签(索引)数组的解决方案:它将为您提供对象
text_and_tags
,如下所示: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:It will get you the object
text_and_tags
as follows:使用以下函数查找与
arr1
中的值匹配的标签索引(来自arr2
数组)。详细解释请关注代码注释。
结果:
参考:
Use following function to find indexes of tags (from
arr2
array) that match values fromarr1
.Follow code comments for detailed explanation.
Result:
Reference: