Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- const makeMeFlat = (arr) => {
- let output = []; //define what we want to eventually return
- if (arr && Array.isArray(arr)) { //make sure there is an input and it's an array
- for(var i = 0; i < arr.length; i++) { //Loop through input
- if (Array.isArray(arr[i])) { //The parameter is an array, we need to look into this; recursively
- output = output.concat(makeMeFlat(arr[i])); //Redefine `output` as itself with the array concactenated to it, while recursively looping through the parameter
- } else {
- output.push(arr[i]); //The parameter is not an array, send it to the output
- }
- }
- }
- return output;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement