Lodash _.toArray() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.toArray(value) picks a strategy based on the input’s shape.
  • Why strings become code points, not raw UTF-16 code units.
  • How Maps yield entry pairs and Sets yield value arrays.
  • That every falsy input short-circuits to an empty [].

Prerequisites

You know what an array-like value is (an object with a numeric length) and have seen Array.from.

  • You’re comfortable with Map, Set, generators, and the iterable protocol.
  • Try-it labs load lodash from the CDN.

Overview

Lodash branches on the input’s shape: falsy → []; string → code-point split; array-like → copy; iterable → iterate; Map → entries; Set → values; everything else → _.values(value).

Object → values

Plain objects fall through to _.values, dropping keys.

Unicode-safe strings

Splits by code point—emoji and surrogate pairs stay intact.

Falsy short-circuit

null, undefined, 0, '', false, NaN all yield [].

Syntax

javascript
_.toArray(value)
  • value: the value to convert.
  • Returns: a new Array. Never mutates the input.
1

Objects, strings, numbers, null

The lodash docs baseline: object values, character split, and the empty-array fallback.

javascript
import toArray from "lodash/toArray";

console.log(
  "object: " + JSON.stringify(toArray({ a: 1, b: 2 })) + "\n" +  // [1,2]
  "string: " + JSON.stringify(toArray("abc")) + "\n" +              // ["a","b","c"]
  "number: " + JSON.stringify(toArray(1)) + "\n" +                   // []
  "null:   " + JSON.stringify(toArray(null))                          // []
);
Try it Yourself
2

Maps, Sets, and generators

Map → entry pairs; Set → values (deduplicated); a generator → whatever it yields.

javascript
import toArray from "lodash/toArray";

function* gen() { yield 10; yield 20; yield 30; }

console.log(
  "map: " + JSON.stringify(toArray(new Map([["a", 1], ["b", 2]]))) + "\n" +  // [["a",1],["b",2]]
  "set: " + JSON.stringify(toArray(new Set([1, 2, 3, 1]))) + "\n" +              // [1,2,3]
  "gen: " + JSON.stringify(toArray(gen()))                                       // [10,20,30]
);
Try it Yourself
3

Unicode-safe string split

_.toArray walks strings by code point, so multi-byte characters survive the trip—exactly where .split('') falls apart.

javascript
import toArray from "lodash/toArray";

const text = "a💖b";

console.log(
  "toArray:    " + JSON.stringify(toArray(text)) + "\n" +    // ["a","💖","b"]
  "split(''):  " + JSON.stringify(text.split("")) + "\n" +    // ["a","\ud83d","\udc96","b"]
  "Array.from: " + JSON.stringify(Array.from(text))           // ["a","💖","b"]
);
Try it Yourself

📋 _.toArray vs related conversions

API / patternBehavior
_.toArray(x)Multi-strategy: object → values, string → code points, Map → entries, Set/iterable → values. Falsy → [].
Array.from(x)Iterable or array-like only; throws for plain objects.
Object.values(x)Just the values—no array-like / iterable handling, throws on null.
[...x]Iterable only; doesn’t accept plain objects or array-likes without an iterator.

Pitfalls to avoid

Falsy

All falsy inputs become []

If you pass 0 or false expecting [0] or [false], you’ll get []. Wrap the value yourself: [value] if that’s what you want.

Numbers

Numbers do not wrap into an array

_.toArray(1) returns [] because numbers aren’t iterable or array-like. Use [n] or _.castArray(n) to keep the value.

Order

Object key order

For plain objects, lodash uses _.values, which follows the standard own-enumerable-key order. Numeric-string keys come first, then insertion order—don’t assume your authoring order.

❓ FAQ

Numbers are neither array-like nor iterable, so lodash falls through to _.values(Number(1)) and the boxed primitive has no enumerable own props.
Yes. For strings it uses code-point iteration (the same logic as the spread operator), so multi-byte characters stay intact: _.toArray('a❤b') returns ['a','❤','b'].
Map values become entry pairs: _.toArray(new Map([['a',1]])) returns [['a',1]]. Sets give you the values array: _.toArray(new Set([1,2])) returns [1,2].
Any falsy input (null, undefined, 0, '', false, NaN) short-circuits to an empty []. Guard against this if you need to distinguish missing data from an empty array.

Summary

  • Purpose: turn any value into a real Array using the right strategy for the input’s shape.
  • Remember: falsy inputs and primitive numbers both yield []; _.castArray is the right tool when you want to preserve them.
  • Next: continue with Lodash _.toFinite()_.toFinite and friends are up next.
Did you know?

_.toArray splits strings the same way the spread operator does—by Unicode code points. So _.toArray('a💖b') returns ['a', '💖', 'b'], while 'a💖b'.split('') would shred the heart into two broken surrogate halves.

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