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 browser

or try sample data

What Is Flatten and Compare?

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.

Flatten and Compare — Code Examples

JavaScript: flat(Infinity) + comparison

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.

Python: itertools.chain for recursive flattening

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().

Recursive flatten — language-agnostic approach

// 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().

Flatten and Compare Gotchas

Flattening destroys structural information

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.

flat() depth matters

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.

Mixed types after flattening

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.

Frequently Asked Questions

What does it mean to flatten an array?

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.

Why flatten arrays before comparing?

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.

Does this tool flatten arrays automatically?

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.

What is the difference between flat() and flatMap()?

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.

Can I compare arrays with mixed nesting depths?

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.