Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. 'use strict'
  2. const makeMeFlat = (arr) => {
  3. let output = []; //define what we want to eventually return
  4. if (arr && Array.isArray(arr)) { //make sure there is an input and it's an array
  5. for(var i = 0; i < arr.length; i++) { //Loop through input
  6. if (Array.isArray(arr[i])) { //The parameter is an array, we need to look into this; recursively
  7. output = output.concat(makeMeFlat(arr[i])); //Redefine `output` as itself with the array concactenated to it, while recursively looping through the parameter
  8. } else {
  9. output.push(arr[i]); //The parameter is not an array, send it to the output
  10. }
  11. }
  12. }
  13. return output;
  14. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement