Last active
February 2, 2017 00:36
-
-
Save crobinson42/aa0f0f446d5f027a1e5d7f10b18a31db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | |
function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); } | |
var _flatten = function _flatten(_ref) { | |
var _ref2 = _toArray(_ref), | |
zeroIndex = _ref2[0], | |
rest = _ref2.slice(1); | |
if (zeroIndex === undefined) return [];else if (!Array.isArray(zeroIndex)) return [zeroIndex].concat(_toConsumableArray(_flatten(rest)));else return [].concat(_toConsumableArray(_flatten(zeroIndex)), _toConsumableArray(_flatten(rest))); | |
}; | |
_flatten([1, 2, [3, 4, [5], 6, [7, [8]]]]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Task: flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4] | |
Spec: Production ready code | |
**/ | |
const _flatten = ([zeroIndex, ...rest]) => { | |
if ( zeroIndex === undefined ) return []; | |
else if ( !Array.isArray( zeroIndex ) ) return [ zeroIndex, ..._flatten(rest) ]; | |
else return [ ..._flatten( zeroIndex ), ..._flatten( rest ) ]; | |
} | |
_flatten([1,2,[3,4,[5],6,[7,[8]]]]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment