如何使用JavaScript生成以’00’开头的UUID v4?

huangapple go评论74阅读模式
英文:

How can I generate a UUID v4 that starts with '00' using JavaScript?

问题

如何生成以特定字符开头的有效UUID v4?

假设我想生成以00开头的UUID v4 – 除了循环生成随机UUID直到获得以00开头的UUID之外,是否有更好的方法?

英文:

How to generate a valid UUID v4 that starts with specific characters?

Suppose I want to generate UUID v4 that starts with 00 – short of looping through random UUIDs until I get UUID that starts with 00, is there a better way of doing it?

答案1

得分: 0

你只需移除生成的 UUID 的前两个字符,并在其位置前置 "00"

console.log('00' + crypto.randomUUID().slice(2));
英文:

You can just remove the first two characters of a generated UUID and prepend "00" in its place.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(&#39;00&#39; + crypto.randomUUID().slice(2));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月22日 09:12:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定