JavaScript
Flatten one level of nested arrays
Call flat with depth 1 to combine one level of nested arrays into a new array. Deeper arrays remain nested, which helps preserve meaningful grouping further down the structure.
Worked example
Node.js example · save as example.mjs and run node example.mjs
const groups = [[1, 2], [3, [4, 5]]];
console.log(JSON.stringify(groups.flat(1)));How it works
Flat reads entries in order and copies one nesting level into the result. In this example the outer groups disappear, but the array containing 4 and 5 remains. You can choose a larger depth when the input schema explicitly calls for it.
Limits and mistakes
Flattening removes structural information and does not recursively inspect object properties. Sparse array holes also need care because flat removes holes at the levels it flattens. Avoid infinite-depth flattening when nesting carries meaning.
Check the result
Expect [1,2,3,[4,5]]. Compare with flat(2) and confirm whether removing the final group would lose information your program needs.
Sources
Reference documentation for the behavior used in this guide: