Flatten and Compare Arrays Online
Compare nested or multi-dimensional arrays. Flatten complex structures and see element-level differences.
🔒 100% private — runs entirely in your browseror try sample data
Compare nested or multi-dimensional arrays. Flatten complex structures and see element-level differences.
🔒 100% private — runs entirely in your browseror try sample data
Flatten and Compare is a technique for normalizing nested, multi-dimensional arrays into flat, single-level sequences before running a diff. When arrays contain sub-arrays at varying depths, a direct comparison can flag structural differences that mask the real content changes. Flattening strips away the nesting so you can focus on what values are present, added, or removed — regardless of how deeply they were originally nested.
This workflow is particularly useful when processing hierarchical data that has been restructured. File system directory listings, organizational charts, category trees, and recursively defined data structures all produce nested arrays. When two versions of such data differ in structure but contain largely the same leaf values, a flatten-then-compare approach cuts through the structural noise and shows you the actual data changes. It is also invaluable when migrating between data formats that represent the same information at different nesting levels.
Paste your two arrays below as JSON. The tool performs a deep structural comparison, highlighting nested differences with path-based context. For a pure flatten-then-compare workflow, use Array.flat(Infinity) in JavaScript or itertools.chain.from_iterable() in Python to flatten your data before pasting. Everything runs in your browser — no data is sent to any server.
const arr1 = [1, [2, 3], [4, [5, 6]], 7];
const arr2 = [1, 2, [3, 4, 5], 6, 7, 8]; const flat1 = arr1.flat(Infinity);
const flat2 = arr2.flat(Infinity);
// flat1: [1, 2, 3, 4, 5, 6, 7]
// flat2: [1, 2, 3, 4, 5, 6, 7, 8] const added = flat2.filter(x => !flat1.includes(x));
const removed = flat1.filter(x => !flat2.includes(x)); console.log("Added:", added); // [8]
console.log("Removed:", removed); // []flat(Infinity) recursively flattens all nesting levels. Use flat(1) to flatten only one level deep, or flat(2) for two levels.
import itertools def flatten(lst): for item in lst: if isinstance(item, list): yield from flatten(item) else: yield item arr1 = [1, [2, 3], [4, [5, 6]], 7]
arr2 = [1, 2, [3, 4, 5], 6, 7, 8] flat1 = list(flatten(arr1)) # [1, 2, 3, 4, 5, 6, 7]
flat2 = list(flatten(arr2)) # [1, 2, 3, 4, 5, 6, 7, 8] added = [x for x in flat2 if x not in flat1]
print(f"Added: {added}") # [8]Python has no built-in deep flatten. A recursive generator with yield from is the idiomatic approach. For one-level flatten, use itertools.chain.from_iterable().
// Generic recursive flatten (JavaScript)
function deepFlatten(arr) { const result = []; const stack = [...arr]; while (stack.length) { const item = stack.pop(); if (Array.isArray(item)) { stack.push(...item); } else { result.unshift(item); } } return result;
} const nested = [1, [2, [3, [4, [5]]]], 6];
console.log(deepFlatten(nested)); // [1, 2, 3, 4, 5, 6]The stack-based approach avoids recursion depth limits and works for arbitrarily deep nesting. Use unshift to maintain element order, or push with a final reverse().
Once you flatten [[1, 2], [3, 4]] into [1, 2, 3, 4], you cannot distinguish it from [[1], [2, 3, 4]] or [1, 2, 3, 4]. If the grouping structure is meaningful (e.g., rows in a matrix, grouped records), compare the nested structure directly instead of flattening.
JavaScript's flat() defaults to depth 1, so [1, [2, [3]]].flat() gives [1, 2, [3]], not [1, 2, 3]. Use flat(Infinity) for complete flattening. Many developers forget this and get unexpected nested arrays in their comparison, leading to false diff results.
Flattening an array like ["a", [1, 2], [{"key": "value"}]] produces ["a", 1, 2, {"key": "value"}] — a mix of strings, numbers, and objects. Comparison tools may treat the string "1" and number 1 as different values. Ensure consistent types in your data before flattening and comparing.
Flattening an array means converting a nested, multi-dimensional array into a single-level array. For example, [1, [2, [3, 4]], 5] flattened becomes [1, 2, 3, 4, 5]. This removes all nesting levels so every element sits at the top level, making element-by-element comparison straightforward.
Flattening before comparison is useful when you care about the values contained in the arrays but not their nesting structure. Two arrays might contain identical data organized at different depths — flattening normalizes them to a common structure before diffing, eliminating structural noise from the results.
This tool compares the arrays you paste in their original structure, with full support for deep nested comparison. For a flatten-then-compare workflow, flatten your arrays first using Array.flat(Infinity) in JavaScript or a recursive generator in Python, then paste the flattened versions.
flat() removes nesting levels from an array without transforming elements. flatMap() first maps each element using a callback function, then flattens the result by one level. Use flat(Infinity) for complete recursive flattening, and flatMap() when you need to transform and flatten in a single efficient step.
Yes. Paste your nested arrays as JSON and the tool will compare them structurally, showing differences at every nesting level with path-based context. Elements at different depths will be highlighted as changes. For a values-only comparison that ignores structure, flatten both arrays before pasting.