Javascript 返回键,用于带下划线的值的模糊搜索

Javascript 返回键,用于带下划线的值的模糊搜索,javascript,underscore.js,Javascript,Underscore.js,我试图返回一个给定值的近似匹配项。 这是我的字典资料 const dict = { "key": "value", "nova": "nov", "euro": "eur" } 有没有更简单的方法来使用下划线或香草js来代替循环?基本上,如果输入字符串与“key”匹配,则返回“value” 例子 input: "nova_w2" output: "nov" input: "nova_2323" output: "nov" input: "novae" output: "

我试图返回一个给定值的近似匹配项。 这是我的字典资料

const dict = {
    "key": "value",
    "nova": "nov",
    "euro": "eur"
}
有没有更简单的方法来使用下划线或香草js来代替循环?基本上,如果输入字符串与“key”匹配,则返回“value”

例子

input: "nova_w2" output: "nov"
input: "nova_2323" output: "nov"
input: "novae" output: "nov"
input: "euro12" output: "eur"

如果你必须检查整本字典,你就不能不循环地完成这项工作。 但这很简单。翻阅字典。如果单词包含键,则返回值

const dict={
“键”:“值”,
“nova”:“nov”,
“欧元”:“欧元”
};
const lookup=word=>{
const match=Object.entries(dict.find)([key,value])=>word.includes(key));
复赛
?匹配[1]
:假;
};
log(查找('nova_w2'));
console.log(查找('nova_2323'));
log(查找('novae');
console.log(查找('euro12'));

log(查找('thishouldnotbefound')如果必须检查整个字典,那么不循环就无法真正做到这一点。 但这很简单。翻阅字典。如果单词包含键,则返回值

const dict={
“键”:“值”,
“nova”:“nov”,
“欧元”:“欧元”
};
const lookup=word=>{
const match=Object.entries(dict.find)([key,value])=>word.includes(key));
复赛
?匹配[1]
:假;
};
log(查找('nova_w2'));
console.log(查找('nova_2323'));
log(查找('novae');
console.log(查找('euro12'));

log(查找('thishouldnotbefound')我们可以使用
object.keys()
在对象中搜索正确的键,然后使用find可以找到匹配的键

let key = Object.keys(dict).find(key => this.value.includes(key))
以下是一个工作示例:

const dict={
“键”:“值”,
“nova”:“nov”,
“欧元”:“欧元”
}
常量输入=document.querySelector('input')
addEventListener('input',function(){
让key=Object.keys(dict.find)(key=>this.value.includes(key))
console.log(dict[key])
})

我们可以使用
object.keys()
在对象中搜索正确的键,然后使用find可以找到匹配的键

let key = Object.keys(dict).find(key => this.value.includes(key))
以下是一个工作示例:

const dict={
“键”:“值”,
“nova”:“nov”,
“欧元”:“欧元”
}
常量输入=document.querySelector('input')
addEventListener('input',function(){
让key=Object.keys(dict.find)(key=>this.value.includes(key))
console.log(dict[key])
})

您能更具体地说明您想要实现的目标吗?看起来您想要将所有映射值放入一个可查询的表单中,然后运行某种Levenshtein距离[对它们进行查询,返回距离最小的键/值。能否更具体地说明您想要实现的目标?看起来您希望将所有映射的值放入一个可查询的表单中,然后运行某种Levenshtein距离[对它们进行查询,返回距离最小的键/值。