Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
assign variable to value of Dictionary Javascript

I am building a dictionary but I would like some of the values to contain variables. is there a way to pass a variable to the dictionary so I can assign a dot notation variable? the variables object will always have the same structure and the dictionary will be static and structured the same for each key value pair. essentially I want to pass the value from the dictionary to another function to handle the data.

main.js

import myDictionary from "myDictionary.js"

const variables ={
item:"Hello"
}
const data = myDictionary[key](variables)
console.log(data)


myDictionary.js

const myDictionary = {
key: variables.item
}

so the log should display hello. I know it willl be something straightforward but cant seem to figure it out.

as always any help is greatly appreciated

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You should modify the dictionary so that it keeps actual callback functions instead. Only then it will be able to accept arguments.

const myDictionary = {
  key: (variables) => variables.item
}

const variables = {
  item: "Hello"
}

const key = "key";
const data = myDictionary[key](variables)
console.log(data)

about 4 years ago · Juan Pablo Isaza Report

0

What you are trying to do is not possible. The myDictionary.js file has no idea whats inside you main file. The only thing you could do would be:

myDictionary.js

const myDictionary = {
    key: "item"
}

main.js

import myDictionary from "myDictionary.js";

const variables = {
    item: "Hello"
};

const data = variables[myDictionary["key"]];
console.log(data);

Also, even though JavaScript does not enforce semi-colons, they will save you a lot of headaches of some stupid rule that breaks the automatic inserter.

about 4 years ago · Juan Pablo Isaza Report

0

I must apologise as when I asked the question I wasn't fully clear on what I needed but after some experimentation and looking at my edge cases and after looking at Krzysztof's answer I had a thought and came up with something similar to this -


const dict = {
 key: (eventData) => {
          return [
            {
              module: 'company',
              entity: 'placement',
              variables: {
                placement_id: {
                  value: eventData.id,
                },
              },
            },
            {
              module: 'company',
              entity: 'placement',
              variables: {
                client_id: {
                  value: eventData.client.id,
                },
              },
            },
          ];
        },
      }

Then I'm getting the data like this -

const data = dict?.[key](eventData)

console.log(data)

I can then navigate or manipulate the data however I need.

thank you everyone who spent time to help me

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!