Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda