所以我有一些如下所示的数据:
stringToSearch = 'this string needs to be searched';
labels = ['keys 1-3','keys 4-6','keys 7-9'];
keywords =
['key1', 'key2', 'key3'],
['key4', 'key5', 'key6'],
['key7', 'key8', 'key9']
];
编辑: 基本上我想要实现的是,在字符串中搜索任何键。然后查找与键所在组相对应的所有标签。
编辑:目标是传入字符串并取回标签。
string='这包含 key5 和 key9';
所以它返回,标签为“key 4-6”和“keys 7-9”
最佳答案
试试这个:
stringsToSearch = ['this string needs to be searched', '...']
keywords = {
firstGroup: {
key1: [],
key2: [],
key3: []
},
secondGroup: {
key4: [],
key5: [],
key6: []
},
thirdGroup: {
key7: [],
key8: [],
key9: []
}
}
$.each(keywords, function(groupName, keyGroup) {
$.each(keyGroup, function(key, foundStrings) {
$.each(stringsToSearch, function() {
if(this.search(key) >= 0)
foundStrings.push(this);
});
});
});
关于javascript - 如何在字符串中搜索多维数组中找到的关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5523663/