Confessions and Confusions of Ashok M A. Personal and Professional Blog.

Ashok Mannolu Arunachalam, How toArraysJS

How to flatten an array in JavaScript

To flat an array is easy in JavaScript using the flat array method.

const names = ['AshKeys', ['Sojiro', ['Seta']]]
console.log(names.flat(2)) // ['AshKeys', 'Sojiro', 'Seta']

By default, it flattens to 1 depth but we do can supply the depth level to be flattened.

If you do not know the depth you want, then it is better to give the max depth possible. Infinity

flat method is quite new to JS as it is specified in ES2019. I had to code the snippet in chrome console./O\