title: Counting elements in JSON using JavaScript
body: <p>I hope someone can point me in the right direction, I've burnt hours trying to solve what I think is a simple problem - I've hunted SO and tried various things that were close but no cigar.</p>
  
  <p>I want to count two things from the sample JSON block below.</p>
  
  <p>First I want to count the number of productLine entries, and second I want to count the number of types - by productLine and in total across all productLine(s).</p>
  
  <p>So I'd like to end up with 3 variables with a number assigned to them to represent these three values.</p>
  
  <p>Is anyone able to help?</p>
  
  <pre><code>var products = {
    "product": "Product Name",
    "productLines": [{
      "productLine": "Line 1",
      "types": ["Type 1", "Type 2", "Type 3", "Type 4"]
    }, {
      "productLine": "Line 2",
      "types": ["Type 5", "Type 6", "Type 7", "Type 8"]
    }, {
      "productLine": "Line 3",
      "types": ["Type 9", "Type 10", "Type 11"]
    }, {
      "productLine": "Line 4",
      "types": ["Type 12", "Type 13"]
    }]
  };
  </code></pre>
  
  <p>Output would be something like:</p>
  
  <pre><code>var productLineCount = 4
  var productLine[0].name = "Line 1"
  var productLine[0].types.count() = 4
  var typesCount = 13
  </code></pre>