Compare arrays and objects with Lodash
Updated August 29, 2026
Install Lodash in the project first (npm install lodash). Lodash's _.isEqual() performs a deep comparison of arrays and objects:
import isEqual from 'lodash/isEqual.js';
isEqual([1, { ok: true }], [1, { ok: true }]); // true
The operator === compares arrays by reference, so two separately created arrays are not equal even when their contents match. _.isEqual() checks nested values, but it may be more work than needed for a flat list. If order matters, compare corresponding items; if it does not, define how duplicates and object identity should be handled before sorting or comparing.
JSON.stringify() is not a general deep-equality algorithm: key order, undefined, dates, non-JSON values, and circular references can change its result. Use a small native comparison for a known data shape or a well-tested library for general nested data, and avoid comparing untrusted objects without considering resource limits.
Sources
related.
Ruslan Osipov
About the author