这是我的代码
var text = input.split(" ");
var wordCount = new Array();
var i;
for(i=0; i<text.length; i++){
if(wordCount.length==0){
wordCount.push(text[i], 1);
}
var index= wordCount.indexOf(text[i]);
现在我有了键的索引,并想找到与该键对应的值。
我可以编写一个自定义顺序方法来获取值,但是有没有一个快捷方式,以便我可以说,
var value = wordCount.getValue(text[i]);
将返回 0 或其他整数
此外,push 未按预期设置格式,我该如何在 2d 数组中推送键(我的单词)及其默认值?
最佳答案
将相应的索引也与值一起存储在数组中,如下所示
for(i=0; i < text.length; i++){
if(wordCount.length==0){
wordCount[i] = text[i];
}
因此,在获取值(value)的同时,您可以做
for(var key in wordCount)
{
alert("key " + key + " has value " + wordCount[key]);
}
关于javascript - 如何在javascript中查找二维数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34258630/