如何在字符串中搜索多维数组中找到的关键字?

时间:2011-04-02 14:14:30

标签: javascript jquery arrays multidimensional-array node.js

所以我有一些看起来像这样的数据:

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

所以它返回,标签'键4-6'和'键7-9'

4 个答案:

答案 0 :(得分:2)

试试这个:

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

答案 1 :(得分:1)

在普通的旧Javascript中我会使用这样的东西:

var stringToSearch = 'this string needs to be key2 searched key4';

var Keywords = function(keywords, label) {
    this.keywords = keywords;
    this.label = label;
}

var keywords1 = new Keywords(['key1', 'key2', 'key3'], 'keys 1-3');
var keywords2 = new Keywords(['key4', 'key5', 'key6'], 'keys 4-6');

var keywordsArray = [ keywords1, keywords2 ];

for (var i=0; i <keywordsArray.length; i++) {
    var keywordsEntry = keywordsArray[i];
    for(var j=0; j <keywordsEntry.keywords.length; j++) {
        // here you got the index of the occuring keyword
        if(stringToSearch.indexOf(keywordsEntry.keywords[j]) > 0) {
            // now do sth. with the label
            alert(keywordsEntry.label);
        }
    }
}

(绝对没有很好的编码,但这是为了给你一个开始。)

答案 2 :(得分:1)

或者可能是这样,因为不清楚你想要什么:

stringsToSearch = ['this string needs to be searched', '...']

keywords = {
    label1: {
        keys: ['key1', 'key2', 'key3'],
        matches: []
    },
    label2: {
        keys: ['key4', 'key5', 'key6'],
        matches: []
    },
    label3: {
        keys: ['key7', 'key8', 'key9', 'key10', 'key11'],
        matches: []
    }
}

$.each(keywords, function(labelName, label) {
    $.each(stringsToSearch, function(_, stringToSearch) {
        $.each(label.keys, function(_, key) {
            if(stringToSearch.search(key) >= 0) {
                label.matches.push(stringToSearch);
                return false;
            }
        });
    });
});

答案 3 :(得分:1)

这是另一种解决方案,基于您最近的编辑:

stringToSearch = 'this string needs to be searched';

labelsToApply = {
    myFirstLabel: ['key1', 'key2', 'key3'],
    anotherLabel: ['key4', 'key5', 'key6'],
    lastLabel:    ['key7', 'key8', 'key9', 'key10', 'key11'],
}

function getLabels(str, labels) {
    var appliedLabels = [];
    $.each(labels, function(labelName, keywords) {
        $.each(keywords, function() {
             if(str.search(this) >= 0) {
                 appliedLabels.push(labelName);
                 return false;
             }
        });  
    });
    return appliedLabels;
}

alert(getLabels(stringToSearch, labelsToApply));