【问题标题】:Is there a simple way in Javascript to encode a string using a dictionary?Javascript中是否有一种简单的方法可以使用字典对字符串进行编码?
【发布时间】:2019-06-02 10:20:06
【问题描述】:

我想使用一个简单的替换代码,带有(也许)一个字典。在 Python 中,这很简单,但我想用 JavaScript 编写。

dict = {}
dict["c"]="f"
dict["u"]="i"
dict["t"]="j" .... 

for x in message:
    print (dict[x].upper())

【问题讨论】:

  • 您是否对如何制作字典感到困惑? stackoverflow.com/questions/7196212/…您的问题对于代码示例不是很具体。
  • 你可以为这个作业创建一个对象。 Javascript 对象就像 python 字典,你可以使用字符串作为键并检索值。

标签: javascript string encoding


【解决方案1】:

let dict = {}
dict["c"]="f"
dict["u"]="i"
dict["t"]="j"

for(let key in dict){
  console.log(dict[key].toUpperCase())
}

【讨论】:

  • 太完美了。谢谢。
猜你喜欢
  • 2011-09-20
  • 1970-01-01
  • 2011-01-30
  • 2016-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多