-2

How do I represent “Keycap Digit One”=1️⃣ in a string?

How can I output 1️⃣ to [9] on the console using escape codes, the same way I can output on the console by using console.log('\u{1F51F}');?
I would also like to be able to output 1️⃣ to [9] in a loop.

user120242
  • 14,918
  • 3
  • 38
  • 52

2 Answers2

0

Retrieved codepoints for the 1, and increment codepoint to get the rest of them.
This KeyCap digit one requires 3 code points to represent, so you need 3 utf codes in sequence, shown at the bottom of the code

var blue1 = Array.from('1️⃣').map(x=>x.codePointAt(0));
//['1️⃣'.codePointAt(0),'1️⃣'.codePointAt(1),'1️⃣'.codePointAt(2)]

console.log(blue1);

for(var i = -1; i < 9; i++){
console.log(String.fromCodePoint(blue1[0]+i, ...blue1.slice(1)))
//[blue1[0]+i,blue1[1],blue1[2]]
}

//requires 3 code points to represent, so utf escape sequence is:
console.log(
"\u0031\ufe0f\u20e3", // keycap 1
"\u{32}\u{fe0f}\u{20e3}",  // keycap 2
"\u{33}\u{fe0f}\u{20e3}"  // keycap 3 ...
)

// output hex values for 3 code points of all
for(var i = -1; i < 9; i++)
console.log((blue1[0]+i).toString(16) + ',' +  blue1.slice(1).map(x=>x.toString(16)).join(','))

// output utf escape sequence
for(var i = -1; i < 9; i++)
console.log('\\u{'+(blue1[0]+i).toString(16) + '}' +  blue1.slice(1).map(x=>'\\u{'+x.toString(16)+'}').join(''))
user120242
  • 14,918
  • 3
  • 38
  • 52
0

I would suggest making a counter that goes from 128287 to 128296 in decimal, then converting to hex. Theoretically, it should be possible to use this for unicode escape codes, but as far as I have tested, it is not possible. The below code

//this is if51f in decimal
var x = 128287;
var y = [];
for(var i = 1; i < 10; i++){
  //x.toString(16) converts decimal to hex
  y.push('<br>u{' + x.toString(16) + '}');
  //add a backslash in front of the u in an ide to see my error
  
  x++;
}
  document.getElementById('output').innerHTML = y;
<p id='output'></p>

I would probably just make an array with all of the values, and cycle through those. The code below does that

var x = ['\u{1f51f}', '\u{1f520}', '\u{1f521}', '\u{1f522}', '\u{1f523}', '\u{1f524}', '\u{1f525}', '\u{1f526}', '\u{1f527}'];
document.getElementById('output').innerHTML = x;
<p id='output'></p>

You should say more in your question, because right now it is rather unclear, and that is causing you to get downvotes

Ian Swift
  • 69
  • 1
  • 10