在 Javascript 中生成一组 UUID [英] Generate an array of UUID's in Javascript
本文介绍了在 Javascript 中生成一组 UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用诸如 uuid
之类的模块如何生成以下结构.
我是否正确地假设这可以通过 Promise
来完成,或者至少可以给出这样的方法.
解决方案
uuid 实际上是一个同步功能,根据文档.所以一个简单的循环就可以:
const uuidv4 = require('uuid/v4');const ids = [];for(让 i=0;i<10;i++){ids.push(uuidv4());}
或者如果你愿意,你也可以做一个单线:
const ids = Array(4).fill().map(() => uuid());
Using a module such as uuid
how could I generate the following structure.
[
'd9baea4e-2436-4481-accb-7c2fe835039e',
'60152666-8c2c-4d33-a5c8-da1dda106c5d',
'c4eaa558-83cc-4b94-9aff-1aefdc204794',
'273998e3-3138-41eb-b740-28ee53f7e344'
]
Would I be right in assuming this could be done with a Promise
or at least could such an approach be given.
解决方案
uuid is actually a synchronous function, per the docs. So a simple loop would do:
const uuidv4 = require('uuid/v4');
const ids = [];
for(let i=0;i<10;i++){
ids.push(uuidv4());
}
Or you can also do a one-liner if you like:
const ids = Array(4).fill().map(() => uuid());
这篇关于在 Javascript 中生成一组 UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文