const flatten = arr => { let outputArr = []; // Loop through each index of the array arr.forEach(item => { // If the current item is not an array, push its value immediately. if(!Array.isArray(item)){ outputArr.push(item); // If the current item is an array, recurse into it. // It's usually better to not use recursion, but in this case, // depth is unknown, and recursion makes sense. } else { // Spread the results of the recursion into the output array. outputArr.push(...flatten(item)); } }); return outputArr; } // Created for use with Node or other CommonJS module.exports = flatten;