This is another version of flattening the nested Arrays. It is most likely to be less efficient then the already accepted solution because it needs more iteration. However, this uses Tail Recursion so you do not have to worry about Dataweave recursion limit and so it can even handle deeper nesting level.
If you need to handle heavily nested Arrays (let's say > 50) you can use this. Otherwise, Shoki's solution is more effective
%dw 2.0
import some from dw::core::Arrays
output application/json
@TailRec()
fun flattenAllLevels(arr: Array) = do {
var flattenedLevel1 = flatten(arr)
---
if(flattenedLevel1 some ($ is Array)) flattenAllLevels(flattenedLevel1)
else flattenedLevel1
}
// Helper function to create deeply nested array.
// for example, calling getDeepArray(5) will create [5,[4,[3,[2,[1,[0]]]]]]
fun getDeepArray(level) =
1 to level
reduce ((item, acc=[0]) -> ( [item] << acc ))
---
flattenAllLevels(getDeepArray(500))