Flatten array using flat method in JavaScript

Let's consider, you have a nested array,

const authors = [
  'Param',
  ['Param', 'Jessi'],
  ['Wilson', ['Clifford', 'Joshua']]
];

How do you flatten the array and remove the nesting? It can be easily done using the array flat method.

By default, it flatten 1 level of nesting,

// Flattening upto 1 level
console.log(authors.flat());

// Output - ["Param", "Param", "Jessi", "Wilson", Array(2)]

If you need to flatten to multiple levels of nesting, you can easily do it by passing the argument,

// Flatterning up to 2 levels of nesting
console.log(authors.flat(2));

// Output - ["Param", "Param", "Jessi", "Wilson", "Clifford", "Joshua"]

Check out the codesandbox and play with it,