Lodash _.toArray() method
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
_.toArray(value) - value: the value to convert.
- Returns: a new
Array. Never mutates the input.
Objects, strings, numbers, null
The lodash docs baseline: object values, character split, and the empty-array fallback.
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)) // []
); Maps, Sets, and generators
Map → entry pairs; Set → values (deduplicated); a generator → whatever it yields.
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]
); Unicode-safe string split
_.toArray walks strings by code point, so multi-byte characters survive the trip—exactly where .split('') falls apart.
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"]
); 📋 _.toArray vs related conversions
| API / pattern | Behavior |
|---|---|
_.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
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 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.
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
Summary
- Purpose: turn any value into a real
Arrayusing the right strategy for the input’s shape. - Remember: falsy inputs and primitive numbers both yield
[];_.castArrayis the right tool when you want to preserve them. - Next: continue with Lodash _.toFinite() —
_.toFiniteand friends are up next.
_.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.
6 people found this page helpful
