I am working on an HTML component with JavaScript. This component is going to appear multiple times on the page (on the order of hundreds or maybe thousands), and I want each of them to have a unique identifier.
So far I am creating the unique IDs (UIDs) by generating a random number for each of the components and assigning it as an ID. Something like this:
var UID = "componente-" + Math.floor(Math.random() * 999999);
miComponente.id = UID;
But this method has major problems because it could be the case that some of the IDs could be repeated. At the very least I should add a check to make sure IDs are not repeated, but apart from that what other method is there to generate unique identifiers? Is there a native function in JavaScript that does it? (something like uniqid
PHP).
There is currently a library to generate UUIDs that implements the RFC4122 standard . Its use is very simple, just add the library and use the methods
v1
andv4
according to the version you want to generate.That depends on what you mean by unique .
Using a numeric variable, and incrementing its value each time you use it, is trivial, as is converting it to a string.
You already have a few hundred million unique identifiers. Every time you execute
++UUID
, the counter is automatically increased.You can reset the generator whenever you want:
UUID = 0;
. Or use a prefix :Or use it in a function:
Here I leave you a function that I have been using for a long time and it works wonders for me to generate unique ids.
Book snippet "JavaScript The Good Parts" (page 41)
Another option is the following:
Use the current date to build an id value which will always be unique each time it is generated .
For this sample, I've borrowed the code from W3Schools and adapted it to better illustrate the point above:
Of course, if it's about security or another feature that unique values must have, this may not be the best option depending on the developer's need .
locutus.io
has published on its website an implementationuniqid en JS
equivalent to that ofuniqid de PHP
Naturally, no, you can't. You would have to build a method that binds the IDs to a namespace and increments with each element.
Instead of reinventing the wheel, you could use the _.uniqueId method of underscore which does exactly what you need.
If they should be unique only on the client side (that is, you don't need something cryptographically secure) you can put a counter to generate the id just like the
uniqueId
Underscore function that @amendadiel commented on, and you save an external library , so if we check the mentioned function, we see is quite simple (edited to remove the Underscore namespace ):In the same way you could use a timestamp and a random number if you want something longer (although with the counter it should work for you):
If you want them for the UI you can use Symbol https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Symbol
No libraries, no uids/string generators
really unique
With a fast equals