LoginSignup
0
0

絵文字等のサロゲートペアを含んだ文字列からランダムに文字を取得する方法

Last updated at Posted at 2023-09-22

結論

コメントで有益なご指摘を頂いております。ご参考に。


// 16 文字
const str = "😀A😁B😂C😃D😄E😅F😆G😇H";

const surrogate = [...str];
const length = surrogate.length; // 16
const max = length - 1; // 15
const index = Math.floor(Math.random() * max); // [0-15]

const char = surrogate[index];

console.log(char);

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0