Skip to content

Instantly share code, notes, and snippets.

@vinaybedre
Last active July 21, 2016 08:51
Show Gist options
  • Select an option

  • Save vinaybedre/429befea2f15f4e8cd951434324122dd to your computer and use it in GitHub Desktop.

Select an option

Save vinaybedre/429befea2f15f4e8cd951434324122dd to your computer and use it in GitHub Desktop.
//ArrayFlatter, flatens array of any nested levels
var arrayFlatter = function(){
/**
* Method to flatten input array.
* @param {string} input - Input array, containing deeply nested array
*/
this.flatArray = function(input){
return new Array(JSON.stringify(input).replace(/[\[\]]/g,""));
}
}
var input = [[1,2,[3]],4];
var arrayFlat = new arrayFlatter();
var output = arrayFlat.flatArray(input);
console.log(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment