I should mention that this will be going into a database so it truly needs to be unique. I need to define the id before it enters the database no questions asked.
for (var i = 0; i < obj.length; i++) {
var id = Date.now();
console.log(id);
}
The problem is, this is the output:
1428356251606
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
...
I was thinking of using Math.random()
but it might in the name of all the Norse gods actually hit the same number twice.
Any idea of how to make this truly unique while sustaining the speed of the for loop?
I should mention that this will be going into a database so it truly needs to be unique. I need to define the id before it enters the database no questions asked.
for (var i = 0; i < obj.length; i++) {
var id = Date.now();
console.log(id);
}
The problem is, this is the output:
1428356251606
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
1428356251607
...
I was thinking of using Math.random()
but it might in the name of all the Norse gods actually hit the same number twice.
Any idea of how to make this truly unique while sustaining the speed of the for loop?
Share Improve this question edited Apr 6, 2015 at 22:04 basickarl asked Apr 6, 2015 at 21:41 basickarlbasickarl 40.6k69 gold badges238 silver badges357 bronze badges 9- 1 I suggest looking at UUIDs over something like that – Sterling Archer Commented Apr 6, 2015 at 21:45
- you want unique numbers or random numbers? – btevfik Commented Apr 6, 2015 at 21:47
- This question is not a duplicate of 'JavaScript closure inside loops'!! Have you read the question? How do you undo a closing of a question? – BassMHL Commented Apr 6, 2015 at 21:47
- @StephLhm it has been re-opened. – Gabriele Petrioli Commented Apr 6, 2015 at 21:47
- To explain why the name of all the Norse gods: JavaScript runs cycles faster than one second, so creating a Now() date in a loop will definitely yield duplicates – Sterling Archer Commented Apr 6, 2015 at 21:49
3 Answers
Reset to default 4You should use i
. i
is guaranteed to be unique for every iteration of your loop.
Depending on how long lasting you want the uniqueness of your ID to be, you can add another unique pound to the ID (Date.now()
is a good candidate, because it's guaranteed to be unique across different runs on the same machine at different times).
How about
var id = Date.now();
for (var i = 0; i < obj.length; i++) {
console.log(id+i);
}
You can force iterate until the ids are repeating
var lastGenerated = 0;
var id = 0;
for (var i = 0; i < obj.length; i++) {
do{
id = Date.now();
}while(lastGenerated == id);
lastGenerated = id;
console.log(id);
}
or you can make like bellow
var seed = Date.now() * 1000;
for (var i = 0; i < obj.length; i++) {
var id = seed + i;
console.log(id);
}
A good solution is create a object to manager the id's ...
(function(w){
var __id = Date.now();
function ID(){ }
ID.prototype.next = function(){
return ++__id;
};
w.ID = new ID();
}(window));
for(var i = 0; i < obj.length; i++){
var id = ID.next();
console.log(id);
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745359410a4624290.html
评论列表(0条)