Skip to content

Instantly share code, notes, and snippets.

View CaspianCanuck's full-sized avatar

Tim Askerov CaspianCanuck

  • London, Ontario, Canada
View GitHub Profile
@CaspianCanuck
CaspianCanuck / array-flattener.js
Created January 13, 2019 02:26
JS prototype method that flattens an array that contains nested arrays
/**
* Flattens the array that contains nested arrays, optionally removing null or undefined elements.
* @example
* // returns [1, 2, 3, null, undefined, 4]
* [[1,2,[3,null],undefined],4].flatten()
* @example
* // returns [1, 2, 3, 4]
* [[1,2,[3,null],undefined],4].flatten(true)
* @param removeBlanks {boolean} If evaluates to true, strips any null or undefined array elements.
* @returns {Array} Flattened array of elements of the original array and any of its nested arrays.