Skip to content

Instantly share code, notes, and snippets.

@AWattNY
Created May 23, 2017 19:03
Show Gist options
  • Save AWattNY/838cec8e84234859beec33a10bf42f68 to your computer and use it in GitHub Desktop.
Save AWattNY/838cec8e84234859beec33a10bf42f68 to your computer and use it in GitHub Desktop.
//Test Case 1
//input >>>>>[1,4,2,4, [8, 10, 7], 3, [2, 12]]
// output >>>>> [1, 4, 2, 4, 8, 10, 7, 3, 2, 12]
//Test Case 2
// //input >>>>>[1, 4, 2, 4, [8, [10, 57, 61], 7], 3, [2, 12]]
// output >>>>> [ 1, 4, 2, 4, 8, 10, 57, 61, 7, 3, 2, 12 ]
//Test Case 3
// //input >>>>>[1, 4, 2, 4, [8, [10, [57, 108, 950], 61], 7], 3, [2, 12]]
// output >>>>> [ 1, 4, 2, 4, 8, 10, 57, 108, 950, 61, 7, 3, 2, 12 ]
const processArray = (array) => {
return array.reduce((accu, curr) => {
accu = ( Array.isArray(curr) ) ? accu.concat(processArray(curr)) : accu.concat(curr);
return accu;
}, []);
};
console.log(processArray([1, 4, 2, 4, [8, [10, [57, 108, 950], 61], 7], 3, [2, 12]]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment