Skip to content

Instantly share code, notes, and snippets.

@mota57
Last active September 9, 2018 13:12
Show Gist options
  • Save mota57/14556820509d3d2fbeefce8b6d2db18e to your computer and use it in GitHub Desktop.
Save mota57/14556820509d3d2fbeefce8b6d2db18e to your computer and use it in GitHub Desktop.
Simple Flat Array
var FlatArray = (function(){
var vm = this;
vm.row = [];
function CallFlatFunction(inputs)
{
var result = FlatFunction(inputs);
vm.row = [];
return result;
}
function FlatFunction(inputs){
for(var i in inputs)
{
var el = inputs[i]
if(isArray(el)){
FlatFunction(el);
} else{
vm.row.push(el);
}
}
return vm.row;
}
function isArray(element)
{
return Object.getPrototypeOf(element).constructor === Array ? true: false;
}
return {
Flat: CallFlatFunction,
}
})();
var test_a = [[1,2,[3, [40]]],4];
console.log(FlatArray.Flat(test_a)); //[ 1, 2, 3, 40, 4 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment