Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const steepArr = [[1, 2, [3]], 4]
- const simpleArr = [1, 2, 3, 4]
- const easyArr = [[1], [2], [3], [4]]
- const stringArr = [["$1"], ["$2"], ["$3"], ["$4"]]
- const flatten = (arr, result = []) => {
- for(let item of arr){
- if (Array.isArray(item)) {
- flatten(item, result)
- } else {
- result.push(item)
- }
- }
- return result
- }
- console.log('*** Results for steepArr: ', flatten(steepArr));
- console.log('*** Results for simpleArr: ', flatten(simpleArr));
- console.log('*** Results for easyArr: ', flatten(easyArr));
- console.log('*** Results for stringArr: ', flatten(stringArr));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement