diff --git a/Recursive/FlattenedArray.js b/Recursive/FlattenedArray.js new file mode 100644 index 0000000000..4bd4f65349 --- /dev/null +++ b/Recursive/FlattenedArray.js @@ -0,0 +1,18 @@ + +// https://flexiple.com/flatten-array-javascript/ +// Flatten an given array to reduce the dimensionality of an array + +const flattened = (arr) => { + const res = [] + arr.forEach(i => { + if(Array.isArray(i)){ + res.push(...flattened(i)); + } + else{ + res.push(i); + } + }); + return res; +} + +module.exports = flattened;