Example 1 — Official Deep Flatten
Arrays nested several levels deep come out as one flat list.
const nested = [1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]];
console.log(R.flatten(nested));
// → [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] How It Works
- Plain items.
1,2, and5are not arrays, so they are copied as they are. - Arrays are opened.
[3, 4]is replaced by its items3and4. - Any depth. Inner arrays like
[10, 11]are opened too, no matter how deep they sit. - New array. The order is kept left to right, and
nesteditself is unchanged.
