Last active
February 14, 2023 13:04
-
-
Save UmairJibran/099e93dca7e6a4fb53e8901a62b2fac7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* | |
| Post With Explanation: https://www.linkedin.com/posts/umairjibran_javassript-activity-7031246560059379712-hPp4 | |
| */ | |
| const flattenArray = array => { | |
| return array.reduce((acc, val) => { | |
| return Array.isArray(val) | |
| ? acc.concat(flattenArray(val)) | |
| : acc.concat(val); | |
| }, []); | |
| }; | |
| const nestedArray = [1, [2, [3, 4], 5]]; | |
| const flattenedArray = flattenArray(nestedArray); | |
| console.log(flattenedArray); // [1, 2, 3, 4, 5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment