简体   繁体   English

如何在 javascript 中使用 Array 作为 Object 键?

[英]How can I use Array as Object key in javascript?

I want to make a dictionary that able to get same result with different keys,我想制作一个能够使用不同键获得相同结果的字典,
so I did something like this in javascript:所以我在 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)
           }
        }

The code above only showing the concept, what I should do to get my expected output below? 上面的代码只显示了这个概念,我应该怎么做才能得到下面的预期 output?
expected output: 预期 output:
 dictionary.CHW.FLW //output: 'Result 1' dictionary.CW.FLW //output: 'Result 1' dictionary.CW.FLOW //output: 'Result 1' dictionary.ABC.EFG //output: undefined

My ultimate goal is to able to get the same output by calling different keys in the dictionary.我的最终目标是能够通过调用字典中的不同键来获得相同的 output 。 Is there any library/Logic that can do so?有没有可以这样做的库/逻辑? Thanks for helping me out!谢谢你的协助!

Plain object can't do that, a Map can:普通的 object 做不到,Map 可以:

The Map object holds key-value pairs and remembers the original insertion order of the keys. Map object 保存键值对并记住键的原始插入顺序。 Any value (both objects and primitive values) may be used as either a key or a value.任何值(对象和原始值)都可以用作键或值。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

you can create computed property in the object.您可以在 object 中创建计算属性。 Here I am assuming you are storing key in variable.在这里,我假设您将密钥存储在变量中。

var dictionary = {
        [CHW] : {
              [CW]: {
                      [FLOW] : 'Result 1'
                    }
                  }
              }

now you can access the way you want.现在您可以按照您想要的方式访问。 dictionary[CHW][CW][FLLOW] give you 'Result 1'字典[CHW][CW][FLLOW] 给你'结果 1'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查数组 Javascript 中是否存在 object 数组键 - How I can check if a object array key exists in Array Javascript 如何从JavaScript中的对象键值返回数组? - How can I return an array from an object key value in JavaScript? 如何在JavaScript对象中将数组设置为键值? - How can I set array as a key value in javascript object? 如何在Javascript对象中使用Unicode字符串键? - How can i use Unicode string key In Javascript Object? 在JavaScript中,如何使用函数参数作为对象的键? - In JavaScript how can I use a function parameter as the key to an object? 如何在 css 中使用 javaScript object 键的值? - How can I use a javaScript object key's value in css? 如何在JavaScript关联数组中使用字符串“ watch”作为键? - How can I use the string “watch” as a key in a JavaScript associative array? 我可以使用Javascript数组对象作为函数中的参数吗? 怎么样? - Can I Use A Javascript Array Object As An Argument In A Function? How? 如何使用Javascript中数组对象的元素 - How can I use an element from an object in array in Javascript 如何检查是否存在具有相同密钥的 object 并使用数组(javascript)中的相同密钥和不同属性进行更新? - How can I check if there's an object with a same key and update with a same key and different properties in array(javascript)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM