在Python中,我有一个随机字典,其中使用元组作为键,并且每个元组映射到某个值。
样例
样例
Random_Dict = {
(4, 2): 1,
(2, 1): 3,
(2, 0): 7,
(1, 0): 8
}
以上示例中的键:(4,2) 值:1
我正在尝试在Javascript世界中复制此内容
这是我想出来的
const randomKeys = [[4, 2], [2, 1], [2, 0], [1, 0] ]
const randomMap = {}
randomMap[randomKeys[0]] = 1
randomMap[randomKeys[1]] = 3
randomMap[randomKeys[2]] = 7
randomMap[randomKeys[3]] = 8
randomMap[[1, 2]] = 3
我在想这是否是最有效的方法。我甚至在考虑是否应该使用一个变量来保存两个数字,这样我就可以在JS中使用字典进行1:1映射。寻求更好的建议和解决方案。
[1, 2]
的实际键名应为"1,2"
;非字符串键名会被转换为字符串。 - jonrsharpe