title: Looping through a triple nested object in Javascript
body: <p>I have declared a triple nested object, like so:</p>
  
  <pre><code>var food = {
      "fruit": {
          "apples": {
              "redApples": 20,
              "greenApples": 30
          },
          "bananas": {
              "yellowBananas": 10
          }
      },
      "grains": {
          "bread": {
              "brownBread": 50
          }
      }
  };
  </code></pre>
  
  <p>I found <a href="https://stackoverflow.com/questions/921789/how-to-loop-through-javascript-object-literal-with-objects-as-members">this question</a>, which worked for iterating through an object of an object, but I'm stuck. How would I iterate through this object?</p>
  
  <p><strong>Update:</strong> For this specific problem, nested for-loops would work fine, like this:</p>
  
  <pre><code>for(var key in food)
      for(var prop in food[key])
          for(var foo in food[key][prop])
              console.log(food[key][prop][foo]);
  </code></pre>
  
  <p>However, there are some good recursive functions below which would do this for indefinitely nested objects.</p>