Recursion Examples

Last Updated on

Recursion in programming can be a mind bender. 
Here are two examples of recursive functions coded in JavaScript. 
These examples are dependent on lodash methods.
Hopefully these code patterns are useful to you. I find them useful as starting points when I have to code some new recursion.

 


This function, “omitDeep” deletes an object property wherever it is found in any node or sub-node of the object.
This is a recursive version of lodash’s ‘omit‘.
This is adapted a little bit from this thread on StackOverflow.

 


This function “printTreePaths” takes an array of tree nodes (such as a representation of a file system or some other hierarchy) and produces an array of the printable paths.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.