Javascript function to flatten nested arrays into a single array

Use Array.prototype.flat() to flatten nested arrays into a single array

JavaScript sample

const nestedArray = [1, [2, [3, 4]]];
const flatArray = nestedArray.flat(2);
console.log(flatArray); // Output: [1, 2, 3, 4]

Leave a comment

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