title: Logic by which we can loop over an object when we aren't certain that whether it will be a single object or an array of objects
body: <p>I have a JSON output that can contain the following types of data:</p>
  <ol>
  <li><p>An array of objects</p>
  <pre><code>var data = {
              &quot;EARNINGS&quot;: [
                     {
                       &quot;PAYMENT&quot;: &quot;1923.08&quot;,
                       &quot;REPORTING_NAME&quot;: &quot;Regular Salary&quot;,
                       &quot;CATEGORY_NAME&quot;: &quot;Standard Earnings&quot;
                     },
                     {
                       &quot;PAYMENT&quot;: &quot;0&quot;,
                       &quot;REPORTING_NAME&quot;: &quot;Spot Bonus&quot;,
                       &quot;CATEGORY_NAME&quot;: &quot;Supplemental Earnings&quot;
                     }
                   ]
  }
  </code></pre>
  </li>
  <li><p>An object:</p>
  <pre><code>  var data = {
         &quot;EARNINGS&quot;: 
                 {
                   &quot;PAYMENT&quot;: &quot;1923.08&quot;,
                   &quot;REPORTING_NAME&quot;: &quot;Regular Salary&quot;,
                   &quot;RUN&quot;: &quot;1923.08&quot;,
                  }
             };
  </code></pre>
  </li>
  </ol>
  <p>How to loop through the &quot;EARNINGS&quot; object? as for the case of array of objects I'm able to do so but in the case of single object I have to check whether it's an array or not.</p>