【发布时间】:2020-12-09 00:07:40
【问题描述】:
我想制作一个能够使用不同键获得相同结果的字典,
所以我在 javascript 中做了这样的事情:
var dictionary = {
[CHW,CW] : { //CHW and CW is ref to same thing
[FLOW,FLW,FW] : 'Result 1' //FLOW, FLW and FW is ref to same thing(Flow)
}
}
上面的代码只显示了这个概念,我应该怎么做才能得到下面的预期输出?
预期输出:
dictionary.CHW.FLW //output: 'Result 1'
dictionary.CW.FLW //output: 'Result 1'
dictionary.CW.FLOW //output: 'Result 1'
dictionary.ABC.EFG //output: undefined
我的最终目标是能够通过调用字典中的不同键来获得相同的输出。有没有可以这样做的库/逻辑? 感谢您帮助我!
【问题讨论】:
-
请详细说明您要做什么以及为什么。
-
只使用值本身就是对象的字符串键
-
如果您希望同一个键返回与另一个键相同的结果,只需将辅助键指向第一个键值即可。
var x = { a: 3 }; x.b = x.a;
标签: javascript arrays json dictionary object