Can arrays be utilized as keys for Objects in JavaScript?

I am looking to create a dictionary in Javascript that can return the same result with different keys. Here is an example of what I have tried:

var dictionary = {
        [CHW,CW] : {                      //CHW and CW refer to the same thing
            [FLOW,FLW,FW] : 'Result 1'    //FLOW, FLW, and FW refer to the same thing (Flow)
           }
        }

The code above illustrates the concept, but how can I achieve my desired 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 main objective is to retrieve the same output by using different keys in the dictionary. Is there any library or logic available to accomplish this task? Thank you for your assistance!

Answer №1

Using a Map is essential for maintaining key-value pairs in JavaScript:

The Map data structure allows for the retention of insertion order and supports both objects and primitive values as keys or values.

More information on Maps from Mozilla

Answer №2

If you want to create a computed property within an object, you can do so by storing the key in a variable. For example:

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

You can then access the property in the following way: dictionary[CHW][CW][FLLOW] will give you the value 'Result 1'

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Canvas only draws outside the table, with the exception of the first one

I am facing an issue with placing multiple signature pads inside table cells. Only the first canvas gets drawn, while the others remain blank. I have checked the mouse/touch events. The events are triggered (up/down/move) and the draw function is called, ...

What sets apart .addEventListener and .on() when it comes to including an event in Bootstrap 5?

I'm currently attempting to attach a listener to my modal in order to implement a certain behavior each time the modal is opened. As per the guidance from Bootstrap 5 documentation, you can achieve this by using: https://getbootstrap.com/docs/5.2/com ...

Tips for eliminating unnecessary module js calls in Angular 9

https://i.sstatic.net/3R7sr.png Utilizing a lazy loading module has been efficient, but encountering challenges with certain modules like all-access-pass and page not found as shown in the image above. Is there a way to effectively remove unused module J ...

Is there a way for me to insert elements into this array?

Having trouble adding another fruit to the array using push method. When running the code, the array remains the same. Can you please assist me? HTML <div id="app"> <ul> <li v-for="fruit in fruits"> {{fruit.name}} < ...

Adjust picture based on the MaterialUI slider's value

I want to incorporate a discrete Slider component from Material UI into my React web app. The goal is to have the user change a picture every time they adjust the slider value, with the new image displayed in a specific div. I am wondering how I can achiev ...

There seems to be an issue with the Url.Action method as it

I'm working with this script: $(function() { $.support.cors = true; jQuery.support.cors = true; $.ajax({ crossDomain: true, type: 'GET', url: 'http://example.com/WCFRESTService.svc/GetCategories&apos ...

Utilizing Angular.js to Retrieve JSON Data Using the HTTP Response Interceptor

When it comes to handling HTTP requests and responses, I have a setup that involves sending requests with a 404 status code while also intercepting them in an Angular Service. res.status(404).send({message: 'O Captain! My Captain!'}); In my Ang ...

The hue of the knob is unchangeable once the server data request is made

Upon receiving data from the server request, I am unable to update the color of the knob to match the image provided at this link screencast. Below is my code: JavaScript Code $scope.options = { /* knob option */ }; $http .get(url) .then(functio ...

Display a dropdown menu when hovering over with a delay

I recently created a basic navigation menu with dropdown functionality using CSS3 initially, but I decided to enhance it by incorporating jQuery to display the dropdown after a set timeframe. However, I am facing an issue where all dropdowns appear when ho ...

Retrieve the player's name from the database using jQuery

How can I retrieve the text value from my scores table in the database? Here is an image of my score table: https://i.stack.imgur.com/CUiMw.png The Player_name is stored as Player_id, which is a foreign key from the players' table. While I c ...

The Django user object cannot be looped through or converted into a serialized form

Just starting out with Django and I'm encountering an issue with my views.py file. def edituser(request, id): member = User.objects.get(id=id) return JsonResponse(member, safe=False) I keep getting the error message: "Object of type User is n ...

Validation of Date in Angular 5 (with specified minimum and maximum dates)

Struggling to find a simple solution for this issue, I have a date input like the following: <input [(ngModel)]="toolDate" type="text" class="tool_input tool_input__date"> To control the input and restrict it to only accept dates, I am using . In m ...

Tips for managing numerous HTTP requests in Angular 6

I have a method that is trying to chain together 3 requests like this: showProfileDetails() { this.getUserInfo(this.currentUser.id).pipe( mergeMap(e => this.getAccounts(this.currentUser.id) ), mergeMap(e => this.getPayments ...

What methods does Json.NET use to implement dependency injection while deserializing?

Is it possible for Newtonsoft.Json to create an object of a class that has no default constructor and uses dependency injection to pass its dependencies? Take the following example: public class SomeFoo { private readonly IFooDependency _dependency; ...

Issue with React Router version 6: displaying an empty page

I am currently grappling with implementing react-router for my react applications. However, I am encountering issues with the routing part as it consistently displays a blank page. Below are snippets of the code from the involved files: index.js import R ...

Having trouble retrieving JSON data using php - Receiving empty results

I have received a JSON data that needs to be parsed using PHP. I am trying to extract a specific piece of information from it. Upon using print_r, here is the output of the JSON: Array ( [deviceId] => 07a9727e-3fe5-4f44-9765-134388241f39 [programId] = ...

Placing emphasis on an object that appears following a period of waiting

I'm currently working on enhancing the accessibility of a slider that will be featured on my website. The goal is to create an effect where, after clicking (or pressing enter) on the first slide, the focus shifts to the second slide element, allowing ...

Microphone Malfunction: Abrupt End of Input Detected

I have been experimenting with SpeechRecognition to incorporate microphone functionality into one of my projects. However, when I check the Chrome Console, it displays the error message: Unexpected end of input const speechRecognition = window.webkitS ...

Utilizing Vue-cli 3: Incorporating static assets with a specific path

My application is located at: https:/firstpath.com/firstsubfolder/app.html When using relative paths, my static assets are being loaded from: https:/firstpath.com/firstsubfolder/img/image.png Is there a way to specify a specific path for my static asse ...

Does CSS delayed visibility transition fail to activate JavaScript transitionend event for elements' visibility changes?

My current approach involves using a clever method to delay the transition of visibility, in combination with opacity. So far, everything is working smoothly: <body> <button>run</button> <div class="box"></div> & ...