Skip to content

Instantly share code, notes, and snippets.

@UmairJibran
Last active February 14, 2023 13:04
Show Gist options
  • Select an option

  • Save UmairJibran/099e93dca7e6a4fb53e8901a62b2fac7 to your computer and use it in GitHub Desktop.

Select an option

Save UmairJibran/099e93dca7e6a4fb53e8901a62b2fac7 to your computer and use it in GitHub Desktop.
/*
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