Javascript 字典值未定义(错误)
我想使用一个选择字符串输入在字典中搜索正确的值,然后我必须将其用于公共API 我的选择输入来自此功能,该功能通过选择启动,其中Javascript 字典值未定义(错误),javascript,angular,dictionary,ionic-framework,Javascript,Angular,Dictionary,Ionic Framework,我想使用一个选择字符串输入在字典中搜索正确的值,然后我必须将其用于公共API 我的选择输入来自此功能,该功能通过选择启动,其中InpBox是选择 public dictionary = {Soccer: "b134", Football: "c568"} ; public optionsFn(): void { this.correct = this.InpBox.toString(); let test = this.di
InpBox
是选择
public dictionary = {Soccer: "b134", Football: "c568"} ;
public optionsFn(): void {
this.correct = this.InpBox.toString();
let test = this.dictionary[this.correct]; // does not work
console.log(this.correct, test)
}
控制台总是返回:
Football,未定义
,但它应该是Football,c568
也许你想改写{“Football”:“c568”}?注意足球周围的引号是从控制台复制的。dict来自另一个操作。请尝试将选项fn
定义为箭头函数:公共选项fn=():void=>{…}
。愚蠢的问题--这是否正确中有空格?打印值的方式,Football,undefined
意味着“Football”后面有一个空格。这肯定不会从This.dictionary
返回正确的值。也许可以试试这个.InpBox.toString().trim()
?这确实是@Pearman提到的空间问题。。。这么愚蠢的错误。非常感谢。