Created
May 12, 2016 13:08
-
-
Save jaimeagudo/71fb7b742c2fd97217ed1e9e42ef0484 to your computer and use it in GitHub Desktop.
JS Bin flatten implementation // source https://jsbin.com/wupeviv
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="flatten implementation"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-source-javascript" type="text/javascript"> | |
var ar=[[1,2,[3],[[[[["2.3"]]]]]],4,4.5,null]; | |
var r=[1,2,3,4]; | |
var flatten=function(a){ | |
var output = []; | |
if(Array.isArray(a)) | |
a.forEach(function(value){ | |
if (Array.isArray(value)) { | |
output=output.concat(flatten(value)) | |
} else if(Math.floor(value) === value){ | |
output.push(value); | |
} | |
}); | |
return output; | |
} | |
console.log(flatten(ar)); | |
console.log(flatten([])); | |
console.log(flatten()); | |
console.log(flatten(1));</script></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment