Table of Contents#
Syntax#
The _.uniq() function has the following syntax:
_.uniq(array, [isSorted], [iteratee], [context])array: The input array from which duplicates will be removed.[isSorted](optional): A boolean value indicating whether the input array is already sorted. If set totrue, the function can optimize its performance.[iteratee](optional): A function that will be called for each element in the array. The return value of this function will be used to determine uniqueness.[context](optional): The context (thisvalue) to use when calling theiterateefunction.
Common Use Cases#
Removing Duplicates from an Array#
The most common use case for _.uniq() is to remove duplicate values from an array. For example:
const numbers = [1, 2, 2, 3, 4, 4, 5];
const uniqueNumbers = _.uniq(numbers);
console.log(uniqueNumbers); // Output: [1, 2, 3, 4, 5]Working with Sorted Arrays#
If you have a sorted array, you can pass true as the isSorted parameter to optimize the performance of _.uniq(). For example:
const sortedNumbers = [1, 2, 3, 4, 5, 5, 6];
const uniqueSortedNumbers = _.uniq(sortedNumbers, true);
console.log(uniqueSortedNumbers); // Output: [1, 2, 3, 4, 5, 6]Using an Iteratee Function#
You can also use an iteratee function to define custom uniqueness criteria. For example, if you have an array of objects and you want to remove duplicates based on a specific property:
const users = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 1, name: 'John' }
];
const uniqueUsers = _.uniq(users, false, function(user) {
return user.id;
});
console.log(uniqueUsers);
// Output: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]Example Usage#
Basic Example#
const fruits = ['apple', 'banana', 'apple', 'orange', 'banana'];
const uniqueFruits = _.uniq(fruits);
console.log(uniqueFruits); // Output: ['apple', 'banana', 'orange']Using Iteratee with Objects#
const products = [
{ category: 'electronics', name: 'laptop' },
{ category: 'clothing', name: 't-shirt' },
{ category: 'electronics', name: 'phone' }
];
const uniqueProductsByCategory = _.uniq(products, false, function(product) {
return product.category;
});
console.log(uniqueProductsByCategory);
// Output: [{ category: 'electronics', name: 'laptop' }, { category: 'clothing', name: 't-shirt' }]Best Practices#
- Understand the Data Type: Make sure you understand the data type of the elements in the array. If you're using an
iterateefunction, ensure it returns a value that can be compared appropriately for uniqueness. - Optimize for Sorted Arrays: If your array is sorted, always pass
trueas theisSortedparameter to take advantage of the performance optimization. - Test Thoroughly: Test your
_.uniq()usage with different types of arrays (including edge cases like empty arrays or arrays with a single element) to ensure it behaves as expected.
Reference#
By understanding the _.uniq() function in Underscore.js, you can effectively remove duplicate values from arrays and work with data in a more organized and efficient manner. Whether you're dealing with simple arrays of primitives or complex arrays of objects, _.uniq() provides a flexible solution for achieving uniqueness.