-1

I want to be able to take a string such as f37f and return "\uf37f" from my function. The strings correspond to icons.

If I return "\uf37f" the function works, but it does not work if I try this:

return `\\u${iconString}`;

The icon shows up as text. I cannot simply concatenate "\u" and my icon as this throws a hexadecimal digit expected error.

user3483203
  • 50,081
  • 9
  • 65
  • 94

1 Answers1

4

You need to use string.fromCharCode:

Example from the documentation:

String.fromCharCode(0x2014)       // returns "—"

That takes a number, not a string, so first you'll need to parse the string into a number, e.g.:

parseInt("f37f")  

You can store that in an intermediate variable or combine them into a single expression.

String.fromCharCode(parseInt("f37f"))