英文:
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('00' + crypto.randomUUID().slice(2));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论