Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@swoleengineer
Created December 8, 2017 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swoleengineer/880c157b3ea5a87549e6d8a85f61a5de to your computer and use it in GitHub Desktop.
Save swoleengineer/880c157b3ea5a87549e6d8a85f61a5de to your computer and use it in GitHub Desktop.
const isFlat = array => {
if (!array) return true;
for (let i = 0; i < array.length; i++) {
if (array[i] instanceof Array) return false;
}
return true;
}
const makeFlat = arr => {
while (!isFlat(arr)) arr = arr.reduce((a,b) => a.concat(b), []);
return arr
}
console.log(makeFlat([]));
console.log(makeFlat([{ payment: 'cash'}, { test: true}, { activity: 'skiing'}]));
console.log(makeFlat([['mixed', 2], 'abc']));
console.log(makeFlat([[[1], 2], 3]));
console.log(makeFlat([[[{ test: true, other: false}], 2], 3]));
console.log(makeFlat([[50], [60], [70]]));
console.log(makeFlat([[[[1], { test: false}], [3]]]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment