Save all meta tags name and value to an object using JavaScript
<p>I need to read all the meta tags on a specific page and store each meta name and content values in a datalayer array that will be used for Google Tag Manager.</p>

<p>I want to fetch all the meta tags then I want to be able to push each value in the array accordingly.</p>

<p>I have the following code but I don't think it is the best way to do it if I have like 20-30 meta tags.I hope someone can help me improve this code!!</p>

<pre><code>&lt;html&gt;
&lt;head&gt;
    &lt;title&gt; test metas&lt;/title&gt;
    &lt;meta name="ABC" content="dummy"/&gt;
    &lt;meta name="DEF" content="dummy"/&gt;
    &lt;meta name="JHK" content="dummy"/&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script type="text/javascript" charset="utf-8"&gt;

function GTMMeta(name) { 
   var metas = document.getElementsByTagName('meta'); 
   for (i=0; i&lt;metas.length; i++) { 
      if (metas[i].getAttribute('name') == name) { 
         return metas[i].getAttribute('content'); 
      } 
   } 
    return '';
} 

  dataLayer = [{}];

if (GTMMeta('ABC') !=''){
    dataLayer.push({'cmsName': GTMMeta('ABC')})
 }; 
if (GTMMeta('DEF') !=''){
  dataLayer.push({'transactionTotal': GTMMeta('DEF')});
}  

if (GTMMeta('JHK') !=''){
  dataLayer.push({'market': GTMMeta('JHK')});
}  
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
