Skip to content

Instantly share code, notes, and snippets.

@paveleremin
Created January 7, 2019 15:57
Show Gist options
  • Save paveleremin/3e06cfbcaf77281de48065c6837a3790 to your computer and use it in GitHub Desktop.
Save paveleremin/3e06cfbcaf77281de48065c6837a3790 to your computer and use it in GitHub Desktop.
function equals(arr1, arr2) {
return JSON.stringify(arr1) === JSON.stringify(arr2);
}
function flatten(arr) {
return arr.reduce((result, toFlatten) => {
return result.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
}, []);
}
const tests = [
{
it: [[1,2,[3]],4],
should: [1, 2, 3, 4]
},
{
it: [[1,0,2,[3]],4],
should: [1, 0, 2, 3, 4]
},
{
it: [[[1,[[0]],2,[3]],4]],
should: [1, 0, 2, 3, 4]
},
{
it: [1],
should: [1]
},
{
it: [],
should: []
},
]
for (let { it, should } of tests) {
console.log(equals(flatten(it), should))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment