Flatten mixed nested arrays and objects of any shape
Write a function named flatten that is capable of taking an input which may be an arbitrarily nested object or array, and return a flattened version of it. In the flattened output, all elements from nested arrays should be in a single-level array, and all nested object properties should be brought to the top level of a single object. For arrays alone, the native Array.prototype.flat() does part of this job, but it does not flatten objects.
The challenge involves creating a versatile function that can handle various types of nested structures, including combinations of arrays and objects, without losing any data in the process. The function should be able to distinguish between objects and arrays and treat them accordingly, ensuring that arrays are merged into a single array and object properties are collated into a single object.
Consider the following examples to understand the expected behavior of the function:
[1, [2, 3], [[4, 5], 6]], the function should produce [1, 2, 3, 4, 5, 6].{ a: 1, b: { c: 2, d: { e: 3 } } }, the expected output is { a: 1, c: 2, e: 3 }.[1, { a: 2, b: [3, 4] }, [5, { c: 6 }]], the function should return [1, 2, 3, 4, 5, 6].[[[[[[[[[[1]]]]]]]]]], the output should simply be [1].null, the function should return null.Your task is to implement the flatten function in such a way that it fulfills the above requirements, providing a simple and efficient solution to the problem of dealing with complex, nested data structures. This is a common challenge that developers face, making it a valuable exercise for enhancing your problem-solving skills and understanding of data manipulation.