I have been working on a JavaScript function to flatten a nested array, but I'm encountering a problem. The function only returns the original array instead of flattening it. For example, when I run the function with the array [1, 2, 3, [4, 5, [6], []]]
, it simply outputs the same array. Even though I am using the .map
method and recursion to manipulate the return values, it's not functioning as expected.
function mapper(array) {
return array.map((item) => {
return (Array.isArray(item)) ? mapper(item) : item
}
)}