JSFiddle - React, Tailwind, and code Playground

by Khương Lâm Ngọc

JavaScript

// Ex1:
const arr1 = [0, 1, 2, [3, 4]];
console.log(arr1.flat());
console.log(arr1.reduce((acc, val) => acc.concat(val), [])); // equivalent to flat
console.log([].concat.apply([], arr1)); // equivalent to flat
// Expected output: [0, 1, 2, 3, 4]

// Ex2:
const arr2 = [0, 1, 2, [[[3, 4]]]];
console.log(arr2.flat(2)); // Expected output: [0, 1, 2, [3, 4]]