This gets an array of keys and loops its values. If these two snippets look okay to you, you can combine them in a recursive function like I did in the example below. I haven't optimized for performance or lines of code; I tried to make it readable. Also note that I'm using Array.isArray and a quick-and-dirty isPrimitive check.