Lodash _.uniq() Method

The _.uniq method is used to create an array of unique values in order, from all given arrays using SameValueZero for equality comparisons.

Syntax:

_.union(array)

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

  • array: This parameter holds array to inspect.

Return Value: This method is used to return the new array of combined values.

Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.



javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
let y = ([1, 2, 2, 3, 4, 3, 8, 6]);    
  
// Use of _.uniq() 
// method
  
let gfg = _.uniq(y);
      
// Printing the output 
console.log(gfg);

chevron_right


Output:

[ 1, 2, 3, 4, 8, 6 ]

Example 2:

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
let y = (['a', 'b', 'c', 'e', 'd', 'd', 'g', 'i', 'i']);    
  
// Use of _.uniq() 
// method
  
let gfg = _.uniq(y);
      
// Printing the output 
console.log(gfg);

chevron_right


Output:

[ 'a', 'b', 'c', 'e', 'd', 'g', 'i' ]

Example 3:

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
let y = (['apple', 'banana', 'banana', 'chikoo',
          'Elderberry', 'Damson 
  
Date', 'guava', 'Damson Date']);    
  
// Use of _.uniq() 
// method
  
let gfg = _.uniq(y);
      
// Printing the output 
console.log(gfg);

chevron_right


Output:

['apple', 'banana', 'chikoo', 'Elderberry', 'Damson Date', 'guava']

Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.

full-stack-img




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.