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

252
Visualizações
How can I convert string into an imported alias in React?

I have got an image folder that contains images of playing cards and I imported them as follow:

import D2 from "../image/D2.png";
import H2 from "../image/H2.png";
import S2 from "../image/S2.png"; 

and all the way until I reach SA (Spade Ace)

I also have key value pair dictionary such as:

  1: "C2",
  2: "D2",
  3: "H2",
  4: "S2",... 

I need this dictionary to both calculate the rank of the card and to generate image.

atm I can retrieve the value by evaluating the last char of the string and convert it into int (in case of K,Q etc I have a switch case for them)

My issue is I have trouble to convert the string into the alias that I imported. I tried doing eval(4) but it didn't work.

if I modified the dictionary into

  1: C2,
  2: D2,
  3: H2,
  4: S2,...

I can easily use this as the src of my images, but I can no longer use the card-code to obtain the value of the card.

Could you please suggest a solution or a better approach for this problem.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If I follow the question properly can't you just create the dictionary in this manner? If you intend to use card codes to obtain the value / image then you could build your dictionary like so:

dict = {
  C2: { image: C2, value: 1 },
  D2: { image: D2, value: 2 },
  ...
}

c2Image = dict['C2'].image;
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