Javascript 尝试循环浏览JSON文件
我正在React中创建一个字典应用程序,我已加载JSON字典,如下所示:Javascript 尝试循环浏览JSON文件,javascript,json,reactjs,Javascript,Json,Reactjs,我正在React中创建一个字典应用程序,我已加载JSON字典,如下所示: { "DIPLOBLASTIC": "Characterizing the ovum when it has two primary germinallayers.", "DEFIGURE": "To delineate. [Obs.]These two stones as they are here defigured. Weever.", "LOMBARD": "Of or pertaining to Lombardy
{
"DIPLOBLASTIC": "Characterizing the ovum when it has two primary germinallayers.",
"DEFIGURE": "To delineate. [Obs.]These two stones as they are here defigured. Weever.",
"LOMBARD": "Of or pertaining to Lombardy, or the inhabitants of Lombardy.",
"BAHAISM": "The religious tenets or practices of the Bahais.",
"FUMERELL": "See Femerell."
}
用户在输入字段中输入一个单词,然后将该值传递到以下函数中,以在JSON中搜索匹配的键。然后将匹配的单词按其各自的值放入结果数组中
handleSearch: function(term) {
var term = term;
var results = [];
for (var key in Dictionary) {
if (Dictionary.hasOwnProperty(key)) {
if (term == Dictionary[key]) {
results.push(Dictionary[key])
}
}
}
console.log(results)
},
然而,我正在努力寻找一种成功的方法,通过它来获得结果。控制台正在记录一个空数组
有人能告诉我哪里出了问题吗?您可以通过添加一个比较函数(在下面的示例中是
compareTerm
函数)来进行更好的匹配。我在这里做的是比较术语是否以dictionary键开头,如果希望它是字符串的任何部分,可以将其从==0
更改为-1
//比较需要添加到某处的函数
函数比较器(术语,比较器){
var shortenedCompareTo=比较
.拆分(“”)
.slice(0,术语长度)
.加入(“”);
返回项.indexOf(shortenedCompareTo.toLowerCase())==0;
}
//只更改了比较函数
handleSearch:功能(术语){
var结果=[];
for(字典中的var键){
if(Dictionary.hasOwnProperty(key)){
if(compareTerm(术语,字典[键]){
结果。推送(字典[键])
}
}
}
控制台日志(结果);
},
您可以通过添加一个比较函数(在下面的示例中是compareTerm
函数)来获得更好的匹配。我在这里做的是比较术语是否以dictionary键开头,如果希望它是字符串的任何部分,可以将其从==0
更改为-1
//比较需要添加到某处的函数
函数比较器(术语,比较器){
var shortenedCompareTo=比较
.拆分(“”)
.slice(0,术语长度)
.加入(“”);
返回项.indexOf(shortenedCompareTo.toLowerCase())==0;
}
//只更改了比较函数
handleSearch:功能(术语){
var结果=[];
for(字典中的var键){
if(Dictionary.hasOwnProperty(key)){
if(compareTerm(术语,字典[键]){
结果。推送(字典[键])
}
}
}
控制台日志(结果);
},
术语的价值是什么?术语的价值是什么?