最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Lodash.uniqueId always returns 1 - Stack Overflow

programmeradmin4浏览0评论

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
 |  Show 11 more ments

2 Answers 2

Reset to default 12

lodash _.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
发布评论

评论列表(0)

  1. 暂无评论