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

247
Views
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 answers
Answer question

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 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!