Underscore.js Array Methods

About This Index
This page is the Underscore.js Arrays index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs.
Learn collection basics on /underscore-js/collection. Use this page when you need an array-specific API such as _.first(), _.compact(), or _.uniq().
Start with _.first(), then _.compact(), _.uniq(), and _.range()—then browse the rest by category.
How to Call an Array Method
Pass the array to _, or chain with the Underscore wrapper:
const nums = [1, 2, 3, 4, 5];
// Functional style
_.first(nums); // 1
_.first(nums, 2); // [1, 2]
_.compact([0, 1, false, 2, "", 3]); // [1, 2, 3]
_.uniq([1, 2, 1, 4, 1, 3]); // [1, 2, 4, 3]
_.chunk(nums, 2); // [[1, 2], [3, 4], [5]]
_.range(5); // [0, 1, 2, 3, 4]
// Chaining
_(nums).rest().first(2).value(); // [2, 3] Underscore.js Array Methods Table
All array method guides in this Underscore.js tutorial series (21 tutorials).
Getting Started
4 methodsRead the ends of an array — first, last, rest, and initial.
| Method | Description | Tutorial |
|---|---|---|
_.first() | Returns the first element, or the first n elements as a new array. | Try it |
_.initial() | Returns everything but the last entry, or all but the last n entries. | Try it |
_.last() | Returns the last element, or the last n elements as a new array. | Try it |
_.rest() | Returns the rest of the elements after the first (alias: _.tail / _.drop). | Try it |
Clean & Flatten
3 methodsDrop falsy values, flatten nests, and remove listed items.
| Method | Description | Tutorial |
|---|---|---|
_.compact() | Returns a copy with all falsy values removed. | Try it |
_.flatten() | Flattens a nested array; pass true for a shallow one-level flatten. | Try it |
_.without() | Returns a copy of the array with listed values removed. | Try it |
Set Operations
4 methodsCombine arrays with union, intersection, difference, and uniq.
| Method | Description | Tutorial |
|---|---|---|
_.union() | Produces the union of arrays — unique values in order of first appearance. | Try it |
_.intersection() | Returns values present in every array you pass. | Try it |
_.difference() | Values from the first array that are not present in the others. | Try it |
_.uniq() | Produces a duplicate-free version of the array (alias: _.unique). | Try it |
Zip, Chunk & Object
4 methodsPair rows, split into chunks, or build objects from key lists.
| Method | Description | Tutorial |
|---|---|---|
_.zip() | Merges arrays as rows — pairs corresponding indexes together. | Try it |
_.unzip() | The opposite of zip — splits grouped tuples back into arrays. | Try it |
_.object() | Converts arrays of keys (and optional values) into an object. | Try it |
_.chunk() | Splits an array into groups of a given size. | Try it |
Index & Search
5 methodsLocate values by equality or by a predicate callback.
| Method | Description | Tutorial |
|---|---|---|
_.indexOf() | Returns the index of the first matching value, or -1. | Try it |
_.lastIndexOf() | Returns the index of the last matching value, searching backward. | Try it |
_.sortedIndex() | Finds the index where a value should be inserted to keep order. | Try it |
_.findIndex() | Returns the index of the first value that passes a predicate. | Try it |
_.findLastIndex() | Returns the index of the last value that passes a predicate. | Try it |
Generate
1 methodBuild integer sequences with range.
Frequently Asked Questions
Start with _.first()
Open the first-elements guide—syntax, examples, and FAQs.
12 people found this page helpful
