Answers.Body: <p>If you want to get the values of your global table field, you need to loop through it.</p>
  
  <p><strong>somewhere in a template:</strong></p>
  
  <pre><code>{% js %}
      var table = '
      {% for row in global.tableFieldName %}
        ({{ row.column1 }} - {{ row.column2 }}){% if not loop.last %},{% endif %} 
      {% endfor %}'
  {% endjs %}
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>&lt;script&gt;var table = '(Value 1-1 - Value 1-2),(Value 2-1 - Value 2-2)';&lt;/script&gt;
  </code></pre>
  
  <p>You should check out the documentation about <a href="https://docs.craftcms.com/v3/table-fields.html#settings" rel="nofollow noreferrer">Craft 3 Table Fields</a>, <a href="https://docs.craftcms.com/v3/dev/tags/js.html" rel="nofollow noreferrer">{% js %} Tag</a> and for removing the trailing comma <a href="https://craftcms.stackexchange.com/questions/2898/remove-trailing-comma-from-an-output-loop">Remove trailing comma from an output loop?</a> </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-06T16:42:51.580Z
Answers.LastActivityDate: 2019-02-07T00:22:11.103Z
Answers.LastEditDate: 2019-02-07T00:22:11.103Z
Answers.UserDisplayName: Marten Brosch
Body: <p>In the <code>Globals</code> I have a <code>field</code> which is of type <code>table</code>. I was trying to access this table in the javascript in order to get the values of particular rows. </p>
  
  <pre><code>&lt;script&gt;
          var myTable = "{{ craft.globals.getSetByHandle('myGlobal').myTable }}"; 
  &lt;/script&gt;
  </code></pre>
  
  <p>It returns a string <code>"Array"</code>. If I put <code>[]</code> around the expression, it will return <code>["Array"]</code>. </p>
  
  <p>Do you know a way to get access to this table? </p>
  
Tags: <javascript><globals><table>
Title: Access table from Globals in script
ThreadText: # Access table from Globals in script
  
  (Asked by: username_0 on 2019-02-06)
  
  In the `Globals` I have a `field` which is of type `table`. I was trying to access this table in the javascript in order to get the values of particular rows.
  
  ```
  <script>
          var myTable = "{{ craft.globals.getSetByHandle('myGlobal').myTable }}"; 
  </script>
  
  ```
  
  It returns a string `"Array"`. If I put `[]` around the expression, it will return `["Array"]`.
  
  Do you know a way to get access to this table?
  
  **Tags:** javascript, globals, table
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-02-06. Score: 1
  
  If you want to get the values of your global table field, you need to loop through it.
  
  **somewhere in a template:**
  
  ```
  {% js %}
      var table = '
      {% for row in global.tableFieldName %}
        ({{ row.column1 }} - {{ row.column2 }}){% if not loop.last %},{% endif %} 
      {% endfor %}'
  {% endjs %}
  
  ```
  
  **Output:**
  
  ```
  <script>var table = '(Value 1-1 - Value 1-2),(Value 2-1 - Value 2-2)';</script>
  
  ```
  
  You should check out the documentation about [Craft 3 Table Fields](https://docs.craftcms.com/v3/table-fields.html#settings), [{% js %} Tag](https://docs.craftcms.com/v3/dev/tags/js.html) and for removing the trailing comma [Remove trailing comma from an output loop?](https://craftcms.stackexchange.com/questions/2898/remove-trailing-comma-from-an-output-loop)
  
  
  ---
  
  