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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:51:27+00:00 2026-05-20T09:51:27+00:00

So, I have 3 variables being passed in: stats (JSON object, shown below) place_type

  • 0

So, I have 3 variables being passed in:

  1. stats (JSON object, shown below)
  2. place_type (string, either “US” or “State”)
  3. factor (string)

I have another variable, answer, whose value depends on the other variables. For example, if place_type = “State” and factor = “factor2”, then

var answer = stats.State.factor2; // equals "E"

I’m trying to see if there’s a way to dynamically assign a variable without needing to use eval().

// Simplified, there are many more than 3 factors
var stats = {
    "US": {
        "factor1" : "A",
        "factor2" : "B",
        "factor3" : "C"
    },
    "State": {
        "factor1" : "D",
        "factor2" : "E",
        "factor3" : "F"
    }
}

Using eval(), this is how it’s done:

eval("var answer = stats." + place_type + "." + factor + ";");

Is this possible in JS without needing eval() or a ton of different IF loops? Thanks in advance.

  • 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-05-20T09:51:27+00:00Added an answer on May 20, 2026 at 9:51 am

    Here you go:

    var answer = stats[place_type][factor];
    

    Live demo: http://jsfiddle.net/simevidas/NsT8Y/


    Consider putting this in a try-catch block since you chain property accessors. If the place_type property does not exist in stats, then stats[place_type] will return undefined, and undefined[factor] will throw an error.

    var answer;
    
    try { 
        answer = stats[place_type][factor];
    } catch(e) {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have two variables: Object obj; String methodName = getName; Without knowing the
I have an object array that is being passed and then split up into
Hey, say I have a url just being passed through my php is there
I have the following string being output into a textarea as a variable in
I have a URL that is being generated using GET variables from a form
I have a total of 8 messages being passed on 4 nodes using MPI.
I have the following program where two variables are to be passed by reference
In C#, I have always thought that non-primitive variables were passed by reference and
I have a variable that's being changed all the time in the course of
I have variables with values like 1.7m 1.8k and 1.2b how can I convert

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.