Question: I have been mostly programming in Python. And this year, I learned how to do recursion using it. 

A really good technique my prof taught me is to use list comprehensions like so:

```
def list_all(obj):  

    if not isinstance(obj, list):  
        return [obj]  
    else:  
        return sum([list_all(x) for x in obj], []) 

```

Now that I am using JS more and more instead of Python, I am wondering what are specific built-in functions you can use in the language to help with recursion. I know that list comprehension are deprecated in JavaScript, so obviously I can't use them anymore. Would map or filter be good substitutes for list comprehensions in tackling recursion? If not, then what?

Answer: 
[Array comprehensions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Array_comprehensions#Differences_to_the_older_JS1.7.2FJS1.8_comprehensions) were proposed and implemented by Mozilla as a part of their "Javascript 1.7" initiative (support of then-upcoming and later abandoned ES4) back in 2007, unfortunately they didn't make their way to the current standard (surely, something like `Object.getOwnPropertySymbolsOrSomeOtherUnreadableMess()` is much more important in terms of redability and efficiency). So, in JS you have to resort to `.map` for this kind of stuff:

```
let list_all = obj => {
   if (!Array.isArray(obj))
        return [obj];
    else
        return [].concat(...obj.map(list_all))
}

```
Well this is a really broad question, but in general, JavaScript lends itself to some very practical functional solutions. In the code below, I've defined `flatten` as a very simple recursive procedure that does a simple case analysis on the head of an Array. To me, this reads better than `[].concat` examples that are provided in other answers - also, there's nothing wrong with `Array.prototype.map` but I don't think there's any gain in using it here.

```js
const flatten = ([x,...xs]) => {
  if (x === undefined)
    return []
  else if (x.constructor === Array)
    return [...flatten(x), ...flatten(xs)]
  else
    return [x, ...flatten(xs)]
}

let result = flatten([1,[2,3],[4,[5,6],7],8])

console.log(result)
// [ 1, 2, 3, 4, 5, 6, 7, 8 ]
```

But, if you want a more in-depth answer, I'd look at an answer I posted here:

* [*Merge/flatten an array of arrays in JavaScript*](https://stackoverflow.com/a/39026201/633183)

I have a couple other answers about recursion and array operations that might be useful to you

* [*How to compare arrays in JavaScript*](https://stackoverflow.com/a/34256998/633183)
* [*Is there a mechanism to loop x times in ES6 (ECMAScript 6) without mutable variables?*](https://stackoverflow.com/a/30452949/633183)
* [*ES6: Merge two arrays into an array of objects*](https://stackoverflow.com/a/39711556/633183)
* [*Merge two object in an array*](https://stackoverflow.com/a/38858508/633183)

I hope you find these helpful. If you have any questions, let me know.