Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7726665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:16:20+00:00 2026-06-01T05:16:20+00:00

I am using jQuery 1.7.1 (but that’s probably irrelevant). I have several JavaScript arrays

  • 0

I am using jQuery 1.7.1 (but that’s probably irrelevant).

I have several JavaScript arrays that are populated in several ways. The arrays, basically, look like this:

var TreeArray = [0,1,2];
var FruitArray = [4,5,6,7];

I have some links that have an item type and an item id:

<a href='?' class='Link' data-itemid='123' data-itemtype='Tree'>elm</a>
<a href='?' class='Link' data-itemid='789' data-itemtype='Tree'>walnut</a>
<a href='?' class='Link' data-itemid='456' data-itemtype='Fruit'>orange</a>
<a href='?' class='Link' data-itemid='111' data-itemtype='Fruit'>apple</a>

When the link is clicked, the ItemType and ItemID are collected and passed to the addToArray() function:

$Links.click(function(e) {
    e.preventDefault();
    var ItemType = $(this).data("itemtype");
    var ItemID = $(this).data("itemid");
    addToArray(ItemType, ItemID);
});

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):

var addToArray = function addToArray(ItemType, ItemID) {
    var WhichArray = ItemType + "Array";
    WhichArray.push[ItemID];
}

I will have quite a few different types of arrays, not just two. I will have quite a few functions.

So, how can I access these arrays dynamically?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:16:21+00:00Added an answer on June 1, 2026 at 5:16 am

    Change your arrays to be properties of an object and then you can access them dynamically by name:

    var arrays = {
        Tree: [0,1,2],
        Fruit: [4,5,6,7]
    };
    
    var addToArray = function addToArray(ItemType, ItemID) {
        arrays[ItemType].push(ItemID);
    }
    

    As an alternative, if TreeArray and FruitArray were global variables, you could also access them off the window object like this without changing the array definitions, though I prefer the first implementation above:

    // global variables
    var TreeArray = [0,1,2];
    var FruitArray = [4,5,6,7];
    
    var addToArray = function addToArray(ItemType, ItemID) {
        var WhichArray = ItemType + "Array";
        window[WhichArray].push(ItemID);
    }
    

    This works because all global variables are implicitly properties of the window object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to find a galleria replacement, hopefully using jQuery but other javascript frameworks
I'm trying to make a drop down menu using javascript/jquery but can't get my
I have some tabs using jQuery UI which work just fine, but I want
I'm not using JQuery but I know they have functions to do this. I
i'm using jQuery for sending ajax requests to the server, but i have some
I have a javascript which receives info from a servlet using jQuery: $.get(authenticate, {badge:$('input#badge').val()},
I'm new to JQuery.But, I am trying to do this using it. I have
I have a form using jQuery that I need to manipulate the incoming data.
I have a simple accordion menu working using jQuery but I'm having trouble styling
I am using jquery blockui but the div that is being covered is very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.