javascriptroom blog

Understanding the Underscore.js `_.uniq()` Function

Underscore.js is a popular JavaScript utility library that provides a wide range of useful functions for working with arrays, objects, and more. One such function is _.uniq(), which is used to create a new array with all duplicate values removed. In this blog post, we'll explore the _.uniq() function in detail, including its syntax, common use cases, best practices, and example usage.

2026-06

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 to true, 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 (this value) to use when calling the iteratee function.

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 iteratee function, ensure it returns a value that can be compared appropriately for uniqueness.
  • Optimize for Sorted Arrays: If your array is sorted, always pass true as the isSorted parameter 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.