Lodash _.isArrayLikeObject() method
What you’ll learn
- How
_.isArrayLikeObject(value)narrows_.isArrayLiketo object-like values. - Why primitive strings fail here while Arrays and
argumentsstill pass. - How boxed strings and typed arrays behave.
- When to pair with
_.isPlainObjector_.isArray.
Prerequisites
You already grasp _.isArrayLike and the difference between object values and primitives.
- You know primitive strings versus
Stringobjects. - You can open Try-it labs in the browser.
Overview
Use _.isArrayLikeObject when you want collection-shaped inputs but need to exclude primitives—especially strings—that still look iterable under _.isArrayLike.
Object gate
Filters out non-objects before evaluating length rules.
Strings split
Primitive strings fail; boxed String objects can still pass.
Arrays included
Arrays remain array-like objects—no contradiction with _.isArray.
Syntax
_.isArrayLikeObject(value) - value: any value to test.
- Returns:
trueifvalueis object-like and array-like by Lodash rules; otherwisefalse.
Shaped objects and Arrays—not primitive strings
POJOs with numeric indices plus length qualify, as do Arrays. A string literal is array-like for _.isArrayLike but not object-like, so it stops here.
import isArrayLikeObject from "lodash/isArrayLikeObject";
isArrayLikeObject({ 0: "x", length: 1 }); // true
isArrayLikeObject([10, 20]); // true
isArrayLikeObject("hi"); // false Arguments objects versus functions
The legacy arguments object is an ordinary object with indices. Functions remain excluded despite arity length.
import isArrayLikeObject from "lodash/isArrayLikeObject";
function demo() {
return isArrayLikeObject(arguments);
}
demo(1); // true
isArrayLikeObject(function () {}); // false
isArrayLikeObject(null); // false Typed arrays, boxed strings, and Maps
Typed arrays are objects with element counts. Boxed strings satisfy object-like rules. Associative Maps rely on size, so they fail the array-like length test.
import isArrayLikeObject from "lodash/isArrayLikeObject";
isArrayLikeObject(new Uint8Array(4)); // true
isArrayLikeObject(Object("ab")); // true (boxed String)
isArrayLikeObject(new Map()); // false 📋 _.isArrayLikeObject vs related helpers
| API | Typical difference |
|---|---|
_.isArrayLikeObject(x) | Object-like and array-like; rejects primitive strings. |
_.isArrayLike(x) | Broader; primitive strings pass. |
_.isArray(x) | Only true Arrays. |
_.isPlainObject(x) | Prototype checks for simple objects—not focused on length. |
Pitfalls to avoid
new String() surprises
Rare in modern code, but object wrappers re-enable array-like detection—stick to primitives at API boundaries when possible.
Name overload
“Array-like object” in conversation sometimes means DOM collections; verify whether you truly need Array instances instead.
Shape versus content
Passing the check does not guarantee every index exists—business validation still matters.
❓ FAQ
Summary
- Purpose: array-like detection limited to object-like values.
- Key split: primitive strings excluded versus
_.isArrayLike. - Next: explore more on Lodash _.isBoolean().
_.isArrayLikeObject combines _.isObjectLike with the same length rules as _.isArrayLike. That is why primitive strings fail here even though they pass _.isArrayLike—a string literal is not “object-like” in Lodash’s sense.
6 people found this page helpful
