Skip to content
Related Articles

Related Articles

Improve Article

Lodash _.uniqueId() Method

  • Last Updated : 09 Sep, 2020

The Lodash _.uniqueId() method is used to create an unique id for some element each time. This method will work for the purpose of assigning a unique id for most use cases, but not with complex projects that require a unique id always even if the project restarts. 

Syntax:

_.uniqueId(prefix)

Parameters: This method accepts a single parameter as mentioned above and described below:

  • prefix: The value to prefix the ID with.

Return Value: This method returns the string unique ID.

Example 1:



Javascript




// Requiring the lodash library  
const _ = require("lodash");  
            
// Use of _.uniqueId()  
// Method without the prefix
let gfg = _.uniqueId(); 
        
// Printing the output  
console.log(gfg);

Output:

1

Example 2:

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
    
// Use of _.uniqueId()  
// Method with prefix
let ans = _.uniqueId("gfg_"); 
  
// Printing the output  
console.log(ans); 
  
let ans1 = _.uniqueId("gfg_"); 
  
// Printing the output  
console.log(ans1);

Output:

gfg_1
gfg_2
My Personal Notes arrow_drop_up
Recommended Articles
Page :