• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

JavaScript生成唯一标识id的几种方法

武飞扬头像
梅坞茶坊
帮助1

在开发中偶尔会遇到需要生成唯一编码的时候,比如对数组的任意一项进行多次增删改,这时就需要给每一项添加唯一标识符来加以区分。

方法一

  1.  
    // 添加唯一编码
  2.  
    function fn_Guid() {
  3.  
    function s4() {
  4.  
    return Math.floor((1 Math.random()) * 0x10000).toString(16).substring(1);
  5.  
    }
  6.  
    return (s4() s4() "-" s4() "-" s4() "-" s4() "-" s4() s4() s4()
  7.  
    );
  8.  
    }
  9.  
    console.log( fn_Guid() );//5ab57ce8-8e90-65ef-1815-6a11102a2300

方法二 指定长度和基数

  1.  
    function fn_Guid(len, radix) {
  2.  
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  3.  
    var uuid = [],
  4.  
    i;
  5.  
    radix = radix || chars.length;
  6.  
     
  7.  
    if (len) {
  8.  
    // Compact form
  9.  
    for (i = 0; i < len; i ) uuid[i] = chars[0 | Math.random() * radix];
  10.  
    } else {
  11.  
    // rfc4122, version 4 form
  12.  
    var r;
  13.  
    // rfc4122 requires these characters
  14.  
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  15.  
    uuid[14] = '4';
  16.  
     
  17.  
    // Fill in random data. At i==19 set the high bits of clock sequence as
  18.  
    // per rfc4122, sec. 4.1.5
  19.  
    for (i = 0; i < 36; i ) {
  20.  
    if (!uuid[i]) {
  21.  
    r = 0 | Math.random() * 16;
  22.  
    uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  23.  
    }
  24.  
    }
  25.  
    }
  26.  
     
  27.  
    return uuid.join('');
  28.  
    }
  29.  
     
  30.  
    console.log( fn_Guid() ); // B0A9158E-C37A-4226-B084-D313383A6E3B
学新通

方法三

  1.  
    function fn_Guid() {
  2.  
    var s = [];
  3.  
    var hexDigits = "0123456789abcdef";
  4.  
    for (var i = 0; i < 36; i ) {
  5.  
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  6.  
    }
  7.  
    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  8.  
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  9.  
    s[8] = s[13] = s[18] = s[23] = "-";
  10.  
     
  11.  
    var uuid = s.join("");
  12.  
    return uuid;
  13.  
    }
  14.  
    console.log( fn_Guid() ) ; // ab683eca-5616-41d7-8d73-307f26957339

方法四 生成标准的id,且方法最简单

  1.  
    function fn_Guid() {
  2.  
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  3.  
    var r = Math.random() * 16 | 0,
  4.  
    v = c == 'x' ? r : (r & 0x3 | 0x8);
  5.  
    return v.toString(16);
  6.  
    });
  7.  
    }
  8.  
    console.log( fn_Guid() ); // 12c96135-d4b6-488f-ba1d-449b27851e0b

方法五 生成标准的uuid,且使用了随机种子,比上一个方法要好

  1.  
    function fn_Guid() {
  2.  
    var d = new Date().getTime();
  3.  
    if (window.performance && typeof window.performance.now === "function") {
  4.  
    d = performance.now(); //use high-precision timer if available
  5.  
    }
  6.  
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  7.  
    var r = (d Math.random() * 16) % 16 | 0; // d是随机种子
  8.  
    d = Math.floor(d / 16);
  9.  
    return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  10.  
    });
  11.  
    return uuid;
  12.  
    }
  13.  
    console.log( fn_Guid() ) ; // 826fe4be-631c-4a63-83e9-96a788e8a569

方法六 生成了不太规范的唯一标识,但是平时使用是最简洁的

  1.  
    var uuid = new Date().getTime() Math.random().toString(36).substr(2);
  2.  
    console.log( uuid ); // 1590753224242oqgomgkv7tp

方法六的扩展 比方法六看起来规范些,但是需要一个格式化时间的函数。

  1.  
    function formatDateTime() {
  2.  
    var date = new Date();
  3.  
    var y = date.getFullYear();
  4.  
    var m = date.getMonth() 1;
  5.  
    m = m < 10 ? ('0' m) : m;
  6.  
    var d = date.getDate();
  7.  
    d = d < 10 ? ('0' d) : d;
  8.  
    var h = date.getHours();
  9.  
    var minute = date.getMinutes();
  10.  
    var second = date.getSeconds();
  11.  
    return y m d h minute second;
  12.  
    }
  13.  
    var uuid = formatDateTime() Math.random().toString(36).substr(2);
  14.  
    console.log(uuid); // 20200529195844692wbkvn5l

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfbiae
系列文章
更多 icon
同类精品
更多 icon
继续加载