Categories
JavaScript Answers

How to fix .flat() is not a function with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *