Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda