0

I want to make a dictionary that able to get same result with different keys,
so I did something like this in 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?
expected 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. Is there any library/Logic that can do so? Thanks for helping me out!

3
  • Please elaborate on what you are trying to do and why. Commented Aug 19, 2020 at 17:07
  • Just use string keys whose values are themselves objects Commented Aug 19, 2020 at 17:07
  • If you want the same key to return the same result as another key, that is simply pointing the secondary key to the first keys value. var x = { a: 3 }; x.b = x.a; Commented Aug 19, 2020 at 17:10

2 Answers 2

2

Plain object can't do that, a Map can:

The Map object holds key-value pairs and remembers the original insertion order of the keys. 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

Sign up to request clarification or add additional context in comments.

2 Comments

Are you saying that if the user made a Map with a key of ["x", "y"]", that they could access in the map mapVar.x and mapVar.y, even though the key, in actuality, is an array and not those unique values?
thanks for helping, it really solved my problem, I can use map.get("example_key") to get what i want now
0

you can create computed property in the 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'

1 Comment

Updated the solution, I was assuming that keys are stored in variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.