Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Flattens an array of arbitrarily nested arrays of integers into a flat array of integers
'use strict';
Array.prototype.flatten = function() {
var flattened = []; {
if (Array.isArray(element)) {
// If the array element is an array,
// flatten it, then push each element
// onto the master flattened array.
element.flatten().map(function(e) {
} else {
return flattened;
var input = [[1,2,[3]],4],
expected = [1,2,3,4],
actual = input.flatten();
// Array comparison in Javascript does not behave as expected
// since arrays are really objects
if (!(actual < expected || actual > expected)) {
console.log('PASS', actual);
} else {
console.log('FAIL, expected', expected, 'but got', actual);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.