Try to use lodash (version 4.17.11)
_.uniqueId()
always returns 1 instead of random 3-digits number.
Also _.uniqueId('prefix')
always returns prefix1.
Is it a problem?
Try to use lodash (version 4.17.11)
_.uniqueId()
always returns 1 instead of random 3-digits number.
Also _.uniqueId('prefix')
always returns prefix1.
Is it a problem?
Share Improve this question asked May 23, 2019 at 13:15 Alexander LadoninAlexander Ladonin 1631 gold badge1 silver badge10 bronze badges 16- 3 Have you tried calling it more than once? – VLAZ Commented May 23, 2019 at 13:16
- 2 "instead of random 3-digits number" wait, why should you get a random three digit number? – VLAZ Commented May 23, 2019 at 13:16
- 1 It says it gives you a unique ID, not that it's a three digit one. – VLAZ Commented May 23, 2019 at 13:17
- 1 The documentation doesn't suggest that you should get a random three digit number. Those are just examples. – J. Stott Commented May 23, 2019 at 13:18
- 1 works for me - getting unique numbers every time – VLAZ Commented May 23, 2019 at 13:19
2 Answers
Reset to default 12lodash _.uniqueId() gives you a unique ID thats it. It will never give you the same ID twice while executing your script (if you restart the script it can and will give you the same output) but there is no random number or no specified number of digits. Just try running it.
_.uniqueId() //1
_.uniqueId() //2
_.uniqueId() //3
If you run a new stance, each time the counter basically would start from 1, and hence you might be getting 1 in your case.
You can use this library uniqid, which would give you a new randomly generated id each time.
import uniqid from 'uniqid';
console.log(uniqid()); // -> 4n5pxq24kpiob12og9
console.log(uniqid(), uniqid()); // -> 4n5pxq24kriob12ogd, 4n5pxq24ksiob12ogl