Last active
October 15, 2021 07:05
-
-
Save technikhil314/feb17e1dadd84fd837e32e12647ecadb to your computer and use it in GitHub Desktop.
Flattern array self implementations
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
let depthTraversed = 1; | |
function flattenArray(arr, depth) { | |
return arr.reduce((acc, current) => { | |
if (Array.isArray(current)) { | |
if (depthTraversed > depth) { | |
acc = acc.concat([current]); | |
return acc; | |
} | |
depthTraversed++; | |
acc = acc.concat(flattenArray(current, depth)); | |
depthTraversed--; | |
} else { | |
acc = acc.concat([current]); | |
} | |
return acc; | |
}, []); | |
} | |
const result = flattenArray([1, 2, [3], [[4]], [5, 6], [[[7]]], [[[[[10]]]]], 2], 2); | |
console.log(JSON.stringify(result)); |
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
function flattenArray(arr){ | |
return arr.reduce((acc, current, index) => { | |
if(Array.isArray(current)){ | |
acc = acc.concat(flattenArray(current)); | |
} else { | |
acc = acc.concat([current]); | |
} | |
return acc; | |
}, []); | |
} | |
flattenArray([1, 2, [3], [[4]], [5,6], [[[7]]]]);// [1,2,3,4,5,6,7] |
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
let depthTraversed = 1; | |
function flattenArray(arr, depth) { | |
let flattenedArray = [] | |
if (!Array.isArray(arr)) { | |
return arr; | |
} | |
for (let i = 0; i < arr.length; i++) { | |
if (Array.isArray(arr[i])) { | |
if (depthTraversed > depth) { | |
flattenedArray = flattenedArray.concat([arr[i]]); | |
} else { | |
depthTraversed++; | |
flattenedArray = flattenedArray.concat(flattenArray(arr[i], depth)); | |
depthTraversed--; | |
} | |
continue; | |
} | |
flattenedArray.push(arr[i]); | |
} | |
return flattenedArray; | |
} | |
const result = flattenArray([1, 2, [3], [[4]], [5, 6], [[[7]]], [[[[[10]]]]], 2], 2); | |
console.log(JSON.stringify(result)); |
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
function flattenArray(arr) { | |
let flattenedArray = [] | |
if (!Array.isArray(arr)) { | |
return arr; | |
} | |
for (let i = 0; i < arr.length; i++) { | |
if (Array.isArray(arr[i])) { | |
flattenedArray = flattenedArray.concat(flattenArray(arr[i])); | |
continue; | |
} | |
flattenedArray.push(arr[i]); | |
} | |
return flattenedArray; | |
} | |
const result = flattenArray([1, 2, [3], [[4]], [5, 6], [[[7]]], [[[[[10]]]]], 2]); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment