Skip to content

Instantly share code, notes, and snippets.

@andrit
Last active December 14, 2018 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrit/cb5c49f473cc4fce5cac487130bd05f6 to your computer and use it in GitHub Desktop.
Save andrit/cb5c49f473cc4fce5cac487130bd05f6 to your computer and use it in GitHub Desktop.
const flattenArr = (a) =>{
	//return a reducer function
	return a.reduce(function(acc, curr) {
		//if current value is an array member, 
		Array.isArray(curr)
			//push the value of the recursed function at the currentValue index and the rest of the array through destructuring
			? acc.push(...flattenArr(curr))
			//else, push the current value into the accumulator array
			: acc.push(curr)

			return acc;
			//in the second callback, we define the accumulator as an empty array, hence the ability to pass the current value into the ccumulator array
		}, [])
	}

		flattenArr([1,2,[3,4,5,6], 7, [8,9]]);
    ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment