The Wayback Machine - https://web.archive.org/web/20240129203822/https://www.geeksforgeeks.org/lodash-_-uniq-method/
Open In App
Related Articles

Lodash _.uniq() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

Syntax:

_.uniq(array);

Parameter:

  • array: This parameter holds an array to inspect.

Return Value:

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

Example 1: In this example, we are getting an array having unique elements because of the lodash _.uniq() method.

javascript




// 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);


Output:

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

Example 2: In this example, we are getting an array having unique elements because of the lodash _.uniq() method.

javascript




// 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);


Output:

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

Example 3: In this example, we are getting an array having unique elements because of the lodash _.uniq() method.

javascript




// 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);


Output:

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

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


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 27 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials