Ramda.js R.unnest() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.unnest(list) flattens a list by one level. Each item that is a list is replaced by its elements; everything else stays as it is (official Ramda docs).

Stable since v0.3.0: R.unnest is not deprecated, experimental, or non-standard. Note: it removes exactly one level of nesting; use R.flatten for all levels.

Syntax

Call R.unnest. Start with this line.

Ramda
R.unnest(list)
CallWhat it does
R.unnest(list)Primary signature.
R.unnest(...)Call the list helper.

Prefer import { unnest } from "ramda" when you want a named import.

Library declarations
Ramda
R.unnest(list)

Parameters

ParameterDescription
listRequired. The array to flatten; items may be lists or plain values

Return Value

Return ValueDescription
*A new array, one level flatter; the input is not changed

Ramda Examples

Example 1 — Official: Remove One Level

The two samples from the Ramda docs: a mixed list, and a list of pairs.

Ramda
console.log(R.unnest([1, [2], [[3]]]));           // [1, 2, [3]]
console.log(R.unnest([[1, 2], [3, 4], [5, 6]]));  // [1, 2, 3, 4, 5, 6]
Try It Yourself

How It Works

  1. Plain item. 1 is not a list, so it is copied as it is.
  2. One wrap. [2] becomes 2, and [[3]] becomes [3] — only the outer brackets go.
  3. Pairs. Each pair in the second list is opened, and the numbers are joined in order.
  4. New array. Both calls return new arrays; the inputs are not changed.

Example 2 — R.unnest vs R.flatten

Same deeply nested list, two different depths.

Ramda
const nested = [1, [2, [3, [4]]]];

console.log(R.unnest(nested));   // [1, 2, [3, [4]]]
console.log(R.flatten(nested));  // [1, 2, 3, 4]
Try It Yourself

How It Works

  1. Top level. nested has two items: 1 and [2, [3, [4]]].
  2. unnest. Opens the second item once, giving 2 and [3, [4]] — the rest stays nested.
  3. flatten. Keeps opening lists until none are left, giving [1, 2, 3, 4].
  4. Choose. Use R.unnest when you know the shape is a list of lists; it never flattens more than you expect.

Example 3 — Join Grouped Values into One List

Students are stored per class. Get one list of every student.

Ramda
const byClass = {
  A: ['Ana', 'Ben'],
  B: ['Cy'],
  C: []
};

console.log(R.unnest(R.values(byClass)));  // ['Ana', 'Ben', 'Cy']
Try It Yourself

How It Works

  1. Get the lists. R.values returns [['Ana', 'Ben'], ['Cy'], []].
  2. Open each list. R.unnest adds 'Ana' and 'Ben', then 'Cy'.
  3. Empty group. Class C’s empty list adds nothing, so it simply disappears.
  4. Reverse trip. This undoes a R.groupBy, though the order follows the groups.

Example 4 — Map, Then Unnest (That’s R.chain)

Split sentences into words. Mapping gives a list per sentence; unnesting joins them.

Ramda
const sentences = ['hi there', 'ramda rocks'];

console.log(R.unnest(R.map(R.split(' '), sentences)));
// ['hi', 'there', 'ramda', 'rocks']

console.log(R.chain(R.split(' '), sentences));
// ['hi', 'there', 'ramda', 'rocks']
Try It Yourself

How It Works

  1. Map. R.map with R.split(' ') gives [['hi', 'there'], ['ramda', 'rocks']].
  2. Unnest. R.unnest removes the extra level and returns four words.
  3. Shortcut. R.chain(fn) does both steps at once and prints the same result.
  4. Rule of thumb. If you write R.unnest(R.map(...)), you can use R.chain instead.

Example 5 — Every Product Ordered, Once

Each order has a list of items. Collect them, flatten, and remove repeats.

Ramda
const orders = [
  { id: 1, items: ['pen', 'ink'] },
  { id: 2, items: ['pen'] },
  { id: 3, items: ['pad', 'ink'] }
];

const allProducts = R.pipe(R.pluck('items'), R.unnest, R.uniq);

console.log(allProducts(orders));  // ['pen', 'ink', 'pad']
Try It Yourself

How It Works

  1. Collect. R.pluck('items') gives [['pen', 'ink'], ['pen'], ['pad', 'ink']].
  2. Flatten. R.unnest turns that into ['pen', 'ink', 'pen', 'pad', 'ink'].
  3. Remove repeats. R.uniq keeps the first 'pen' and 'ink', giving three products.
  4. Reusable. R.pipe joins the steps into one function you can call on any order list.

Browser & Runtime Support

R.unnest() is plain ES5 JavaScript inside Ramda 0.3.0+. Unlike native arr.flat(), which arrived in ES2019 (Chrome 69, Firefox 62, Safari 12, Node.js 11), it also runs in older engines.

✓ Ramda 0.3.0+ · Any JS runtime

Ramda.js R.unnest()

The UMD build (ramda.min.js) is compiled to ES5 syntax, so it runs in Chrome, Firefox, Safari, and Edge. Via npm it works in Node.js, Deno, and Bun. The examples on this page only add const (ES2015), which every modern browser supports.

100% With Ramda loaded
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
R.unnest() Universal

Bottom line: Safe in any project with Ramda 0.3.0 or later (check with npm ls ramda). These labs load [email protected].

Frequently Asked Questions

R.unnest(list) removes one level of nesting. Every item that is a list is replaced by its elements; other items are kept as they are. The official sample R.unnest([1, [2], [[3]]]) returns [1, 2, [3]].
R.unnest removes exactly one level. R.flatten removes every level. For [1, [2, [3, [4]]]], R.unnest gives [1, 2, [3, [4]]] while R.flatten gives [1, 2, 3, 4].
R.unnest is R.chain(R.identity). R.chain(fn, list) maps each item with fn and then unnests the result, so R.chain(fn, list) equals R.unnest(R.map(fn, list)).
Almost. Both flatten one level. Differences: arr.flat() skips holes in sparse arrays while R.unnest turns them into undefined, and R.unnest also opens array-like objects such as arguments. arr.flat() needs ES2019; R.unnest runs in ES5 environments.
No. It returns a new array. The copy is shallow, so objects inside are shared with the original. Passing null or undefined throws a TypeError.
No. R.unnest has been a stable Ramda list function since v0.3.0. It is not deprecated, experimental, or non-standard.

Next: R.update()

Replace one item at a given index without changing the original list.

R.update() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful