Sometimes, we want to fix .flat() is not a function with JavaScript.
In this article, we’ll look at how to fix .flat() is not a function with JavaScript.
How to fix .flat() is not a function with JavaScript?
To fix .flat() is not a function with JavaScript, we can use the array reduce method instead of flat.
For instance, we write
const flatArr = result.reduce((acc, curr) => acc.concat(curr), []);
to call reduce with a callback that calls acc.concat(curr) to return an array with all the arrays in result combined into one flattened array with concat.
The 2nd argument is an empty array so acc‘s initial value is an empty array.
Conclusion
To fix .flat() is not a function with JavaScript, we can use the array reduce method instead of flat.