Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

116
Vistas
Create unique identifier with custom format using Plain Javascript

I know that the format of uuid is XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX. But I dont want to create a uuid, I want to create a unique identifier with a different format such as XXX-XXXX-XXX.

How do I do that using plain javascript and no libraries.

And is there any way to ensure that the identifier will be unique?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This is an example of how to create a unique fake uuid looking string. Note that this is not a real uuid.

let uuids = [];

function getRandomUuidChar(count = 1) {
  // valid uuid chars: 0-9, a-f, A-F
  const chars = "1234567890abcdefABCDEF".split('');
  let retval = "";
  for (let i = 0; i < count; i++) {
    const randomIndex = Math.floor(Math.random() * chars.length);
    retval += chars[randomIndex];
  }
  return retval;
}

function generatePseudoUuid() {
  //  XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
  const charCounts = [8, 4, 4, 4, 12];
  const uuid = charCounts
    .reduce((retval, count) => {
      retval.push(getRandomUuidChar(count));
      return retval;
    }, [])
    .join("-");
  if (!uuids.includes(uuid)) {
    uuids.push(uuid);
    return uuid;
  }
  generateUuid();
}
for(let i=0; i<10; i++){
  console.log("uuid", generateUuid());
}
console.log(uuids);

output:

uuid 05D9F4C4-ddd8-9ff4-B5F3-c834E5da6F64
uuid 10dc0BCb-1DfD-8efB-EA7c-3f056930315C
uuid 8e5a21C0-EDD9-f3bc-bEBb-bbADe542342E
uuid b484Ca84-Ce89-eb91-94F4-8216b86A7245
uuid De70ABB8-D4E9-DBba-7783-b8109E1b5E27
uuid F1BF66b8-5458-B8d1-DA0c-6BceBea2ccc0
uuid aEffE093-faf8-FBA2-bb88-08865b446412
uuid a3AabDBC-2B4A-AcBc-8afF-8c0deCCeA92C
uuid C7b4DFaA-Da41-041a-FFfE-28064ABF5FB7
uuid E3F2F592-3AEc-aBdD-AC9c-304Dc93ceDa9
[
  '05D9F4C4-ddd8-9ff4-B5F3-c834E5da6F64',
  '10dc0BCb-1DfD-8efB-EA7c-3f056930315C',
  '8e5a21C0-EDD9-f3bc-bEBb-bbADe542342E',
  'b484Ca84-Ce89-eb91-94F4-8216b86A7245',
  'De70ABB8-D4E9-DBba-7783-b8109E1b5E27',
  'F1BF66b8-5458-B8d1-DA0c-6BceBea2ccc0',
  'aEffE093-faf8-FBA2-bb88-08865b446412',
  'a3AabDBC-2B4A-AcBc-8afF-8c0deCCeA92C',
  'C7b4DFaA-Da41-041a-FFfE-28064ABF5FB7',
  'E3F2F592-3AEc-aBdD-AC9c-304Dc93ceDa9'
]
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda