2011-04-02 64 views
2

所以我有一些数据,看起来像这样:如何在多维数组中找到关键字的字符串?

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 ='this contains key5 and key9';

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

+3

你是什么意思,“标签上的串”? – Pointy 2011-04-02 14:20:03

+0

我不确定我究竟该怎么做。计划在弄清楚如何找到正确的值来标记它之后解决这个问题。 – fancy 2011-04-02 14:25:49

+0

编辑该部分,对此感到遗憾。 – fancy 2011-04-02 14:27:52

回答

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

我认为这绝对会做到这一点,但我有一个问题。标签(以及键)是用户提交的字符串。将它们注入散列表的最佳方法是什么? – fancy 2011-04-02 16:44:32

+0

'labelsToApply ['newLabel'] = newKeys'? – Eric 2011-04-02 16:45:36

+0

好吧,我可能不完全清楚哈希表是如何工作的。我可以通过声明'labelsToApply ['newLabel'] = ['new','Keys']'来传递新数据吗?是否需要按照特定的方式格式化“newLabel”,例如:没有空间? – fancy 2011-04-02 16:54:45

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

这需要唯一的事情是跟踪组名称,但这应该很简单。跟踪搜索到的每个目标所在字符串中的位置也是很好的做法。 – Pointy 2011-04-02 14:44:11

+1

你是什么意思“跟踪组名称”?它通过存储与组内对象匹配的字符串来实现这一点 – Eric 2011-04-02 14:47:56

+0

啊我明白你的意思了 - 不用担心:-)(仍然可能会很好地保持子字符串的索引) – Pointy 2011-04-02 14:55:29

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

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

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; 
      } 
     }); 
    }); 
}); 
相关问题