Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use access arrays dynamically?
    primarykey
    data
    text
    <p>I am using jQuery 1.7.1 (but that's probably irrelevant).</p> <p>I have several JavaScript arrays that are populated in several ways. The arrays, basically, look like this:</p> <pre><code>var TreeArray = [0,1,2]; var FruitArray = [4,5,6,7]; </code></pre> <p>I have some links that have an item type and an item id:</p> <pre><code>&lt;a href='?' class='Link' data-itemid='123' data-itemtype='Tree'&gt;elm&lt;/a&gt; &lt;a href='?' class='Link' data-itemid='789' data-itemtype='Tree'&gt;walnut&lt;/a&gt; &lt;a href='?' class='Link' data-itemid='456' data-itemtype='Fruit'&gt;orange&lt;/a&gt; &lt;a href='?' class='Link' data-itemid='111' data-itemtype='Fruit'&gt;apple&lt;/a&gt; </code></pre> <p>When the link is clicked, the ItemType and ItemID are collected and passed to the addToArray() function:</p> <pre><code>$Links.click(function(e) { e.preventDefault(); var ItemType = $(this).data("itemtype"); var ItemID = $(this).data("itemid"); addToArray(ItemType, ItemID); }); </code></pre> <p>Here's where the problem is. If the ItemType is "Tree", I want to put the ItemID in the TreeArray. If the ItemType is "Fruit", I want to put the ItemID in the FruitArray. Here's my function (that doesn't work):</p> <pre><code>var addToArray = function addToArray(ItemType, ItemID) { var WhichArray = ItemType + "Array"; WhichArray.push[ItemID]; } </code></pre> <p>I will have quite a few different types of arrays, not just two. I will have quite a few functions. </p> <p>So, how can I access these arrays dynamically?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload