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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:17:38+00:00 2026-06-13T01:17:38+00:00

I would like to dynamically generate an integer (in the below case either 100

  • 0

I would like to dynamically generate an integer (in the below case either 100 or 500) and use it to access separate arrays. In a later step (not part of the code below) I would also like to access different parts of those arrays in the same manner (“message 1, 2 or 3”).

For this proof of concept I did not dynamically generate an integer, but I just set it to 100.

I then tried to use eval() to dynamically generate the array name consisting of “warning” and 100, but it is not working properly.

This is my code:

// two arrays are defined, warning100 and warning500
var warning100 = [
    { "message1":"Ok, go ahead and start typing!" },
    { "message2":"Keep going!" },
    { "message3":"You can do it!" }
];

var warning500 = [
    { "message1":"Slow down..." },
    { "message2":"That's it!" },
    { "message3":"Maximum reached." }
];

// set i to 100 and h to 1 for testing purposes, will be random integers in the final version
var i = 100;
var h = 2;
// create variable names as a combination of a string and i or h
// those variables will be used to access one of the arrays from above and one of the messages;
eval("var warningNumber = warning" + i + ";");
eval("var messageNumber = message" + h + ";");

/* alternative code for creating the two variable values
var warningNumber = "warning" + i;
var messageNumber = "message" + h;
*/

// the variable warningNumber from above is now used again to access the array warning100
// the varaible messageNumber is used to access one of the messages
// within that array message1 should be displayed
// create variable to be used in the document.write below
var warning = warningNumber[0].messageNumber;

// should alert "Ok, go ahead and start typing!"    
alert(warning);
  • 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-13T01:17:40+00:00Added an answer on June 13, 2026 at 1:17 am

    Why not just make each set of warnings part of a warningNumber object? That way you could do

    var warnings = {100: { 1:"Ok, go ahead and start typing!",
                           2:"Keep going!",
                           3:"You can do it!"
                         },
                    500: { 1:"Slow down...",
                           2:"That's it!",
                           3:"Maximum reached."
                         }
                   };
    alert(warnings[i][h]);
    

    That way all the eval doesn’t even has to be done.

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

Sidebar

Related Questions

I would like to use the data.table package in R to dynamically generate aggregations,
I would like to use following sql to avoid constructing sql dynamically: SELECT CommentID,
I am trying to dynamically generate linqtosql query using LinqKit . I would like
I would like to dynamically generate a form from a database in ASP.NET, what
I am using ReportLab to generate a pdf dynamically with python. I would like
I would like to dynamically allocate memory from the machine_init function in my arm
For debugging / performance tests I would like to dynamically add logging code to
I created Extjs.Panel and now I would like to dynamically load a content to
I am working on a children's book app and would like to dynamically populate
I am working on a children's book app and would like to dynamically populate

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.