How to generate short uid like “aX4j9Z” (in JS)

前端 未结 10 1577
鱼传尺愫
鱼传尺愫 2020-12-12 14:47

For my web application (in JavaScript) I want to generate short guids (for different objects - that are actually different types - strings and arrays of strings)

I w

10条回答
  •  春和景丽
    2020-12-12 15:14

    You can use the md5 algorithm for generating a random string. md5 is the node package

     var randomChars = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 2);
     var shortUrl = md5(originalUrl + randomChars + new Date()).substring(0, 5).toString();
     console.log(shortUrl);
    

    This will generate unique string every time.

提交回复
热议问题