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

137
Vistas
convert template literal to retrieve object's value without eval
// declared read-only variable:
const support = {
        arr: {
            ins: ['g3', 'g5', 'g7', 'g10',
                'g12', 'g13', 'g14', 'g15'
            ]
        }
    },
// object destructuring for easy to call:
{ arr: { ins: supE1range } } = support;

and a function to dynamically change between sup and 1 in supE1range object:

function autoSelect(currClass, currElite) {
    let setClass = '';
    switch (currClass) {
        case 'support':
            setClass = 'sup';
            break;
    }
    let setObj = `${setClass}E${currElite}range`;
    console.log(eval(setObj));
}
autoSelect('support', 1);

as you can see eval(setObj) will get supE1range's value because the string is match to the object name.

and now how to accomplish that without eval?
I've searching related questions/answers but doesn't satisfy want i need.
umm.. don't mind about 'dynamically change' because it's just part of the code that is unnecessary to put here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

My suggestion is instead of creating a variable from destructuring, try creating an object (map in this case) to hold the value with keys you specify.

Then retrive the value by looking up map object.

const map = {};

// declared read-only variable:
const support = {
    arr: { ins: map.supE1range = ['g3', 'g5', 'g7', 'g10', 'g12', 'g13', 'g14', 'g15'] },
};

function autoSelect(currClass, currElite) {
    let setClass = '';
    switch (currClass) {
        case 'support':
            setClass = 'sup';
            break;
    }
    let setObj = `${setClass}E${currElite}range`;
    console.log(map[setObj]);
}
autoSelect('support', 1);


Or you could declare the variable using var and then retrive the value using window object(not recommended)

// declared read-only variable:
var support = {
        arr: {
            ins: ['g3', 'g5', 'g7', 'g10',
                'g12', 'g13', 'g14', 'g15'
            ]
        }
    },
// object destructuring for easy to call:
{ arr: { ins: supE1range } } = support;

function autoSelect(currClass, currElite) {
    let setClass = '';
    switch (currClass) {
        case 'support':
            setClass = 'sup';
            break;
    }
    let setObj = `${setClass}E${currElite}range`;
    console.log(window[setObj]);
}
autoSelect('support', 1);

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