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 7745397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:00:27+00:00 2026-06-01T10:00:27+00:00

so I find myself in the awkward situation where I have a function in

  • 0

so I find myself in the awkward situation where I have a function in main.js which requires an array that’s populated in second.js…

Simply put, the function in main.js is intended to be reusable:

function chug()
{
    p1.innerHTML = st_a[0];
    p2.innerHTML = st_a[1];
    ...
}

Now, ‘st_a’ is supposed to be resolved as: st_ + , in this case that variable is ‘a’. The idea being the second.js file will have multiple arrays (st_a, st_b, etc.) and depending on the need, the relevant array will be used to populate the paragraph elements (p1, p2, etc.)

Any ideas?

  • 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-01T10:00:30+00:00Added an answer on June 1, 2026 at 10:00 am

    If st_[x] is a global variable, you can use window['st_a'] to reference it. So, you should be able to use something like:

    function chug()
    {
        var arrid = 'st_'+'a';
        p1.innerHTML = window[arrid][0];
        p2.innerHTML = window[arrid][1];
        //...
    }
    

    or use a function to retrieve the array:

    function getArr(id){
      return window['st_'+id];
    }
    

    Alternatively you could use a container object in second.js with a ‘get’ method, something like:

    var myArrays = {
      st_a: [],
      st_b: [],
      st_c: [],
      get: function(id){
        return this['st_'+id];
      }
      /* etc */
    }
    

    Now chug could look like:

    function chug()
    {
        var arr = myArray.get('a');
        p1.innerHTML = arr[0];
        p2.innerHTML = arr[1];
        //...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself in the situation where I need to call a PHP function
I find myself writing code that looks like this a lot: set<int> affected_items; while
I find myself constantly pressing ctrl-z to undo the automatic formatting that happens in
Often when I'm using Python I'll find myself writing list comprehensions that look something
I find myself in conflict, regarding which code style I should follow when using
I find myself typing Ctrl+Shift+O several times to organize imports (usually imports which are
I find myself needing to have a View expose its Model and Controller references.
I find myself using this logic a lot: if (isset($array[$key])) $array[$key] += $num; else
I find myself currently working on several projects which include user entries in a
Here is something that I find myself using from time to time and I

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.