Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Arbitrarily nested array to flattened array. Mutates.
- const flatten = arr => {
- for (let i=0; i < arr.length; Array.isArray(arr[i]) && arr.splice(i, 1, ...arr[i]) || i++){}
- return arr
- }
- // testing
- const nested = [[1,2,[3]], 4]
- const flattened = [1,2,3,4]
- JSON.stringify(nested) !== JSON.stringify(flattened)
- JSON.stringify(flatten(nested)) === JSON.stringify(flattened)
- JSON.stringify(flatten(flattened)) === JSON.stringify(flattened)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement