LoginSignup
0
1

JavascriptでUUID4を取得する

Last updated at Posted at 2024-06-19
function uuid4(){
	return [...new Array(36)].map((_,i) => {
		switch(i){
			case 8:
			case 13:
			case 18:
			case 23:
				return '-';
			case 14:
				return '4';
			case 19:
				return parseInt(Math.random() * 4 + 8, 10).toString(16);
			default:
				return parseInt(Math.random() * 16).toString(16);
		}
	}).join("");
}

自分用メモ。

"RRRRRRRR-RRRR-4RRR-rRRR-RRRRRRRRRRRR".replaceAll(/R/g,()=>parseInt(Math.random()*16).toString(16)).replaceAll(/r/g,()=>parseInt(Math.random()*4+8).toString(16))

単行実装ならこっち。

0
1
1

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
1