Flatten Nested Arrays

Avito
You need to write a function flatten(array) that flattens nested arrays into a single array. Data of other types should remain unchanged. The solution must handle any nesting depth (i.e., it should not contain recursive calls).
Constraints:
  • - Recursion cannot be used
  • - The built-in method Array.prototype.flat() cannot be used
  • - No array modification methods can be used after creation except pop/push

Examples:

Input 1: [0, [1, [2, 3]], 4]
Output 1: [0, 1, 2, 3, 4]
Input 2: [1, "string", [2, [3, "4"]], { a: 1 }]
Output 2: [1, "string", 2, 3, "4", { a: 1 }]
Input 3: [[], [1], [[2, 3]], [[4], [5, [6]]]]
Output 3: [1, 2, 3, 4, 5, 6]
Input 4: [1, [2, [3, [4], [5, [6], [7, [8, [9]]]]]]]
Output 4: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Input 5: [1, [null, undefined], [[true, false]]]
Output 5: [1, null, undefined, true, false]
Run your code to see results.