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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:35:51+00:00 2026-05-18T20:35:51+00:00

My JSON response looks like this : {sample:[{id:2,name:branch name},{id:3,name:branch name 2}]} My function looks

  • 0

My JSON response looks like this :

{"sample":[{"id":"2","name":"branch name"},{"id":"3","name":"branch name 2"}]}

My function looks like this :

function getJSONObjects(){
        $.getJSON("http://localhost/api/branches",
        function(data){
          $.each(data.sample, function(i,item){
           var loc = "branch";
           eval("var " + loc + item.id + "=123;");
           alert(loc + item.id);
          });
        });
 }

The idea is to create branch + id object so I can do something with it(create marker on a map), so I tried to assign it any value to see if this was working.

I wanted both branch2 and branch3 to alert 123 so I have something to start with. But currently this alerts branch2 and branch3 instead of 123.

I have little experience with creating dynamic variables/objects can someone tell me what I’m doing wrong or maybe another approach towards solving this?

  • 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-18T20:35:52+00:00Added an answer on May 18, 2026 at 8:35 pm

    No idea what you want to do here:

    eval("var " + loc + item.id + "=123;"); // eval is EVIL
    alert(loc + item.id); // just creates a string...
    

    Creating dynamic variables is a bad idea. Rather create an object and use key/values.

    var branches = {}; // new object, move this to the scope you want to access the value from later
    branch[item.id] = 123; // set the key 'item.id' to the value '123'
    console.log(branch[item.id]); // retrieve the value of the key 'item.id'
    

    But if you’re doing this you can just as well change the structure of your JSON data to something like this:

    {"sample":[{"1": {"name": "branch name1", "value": 123}, "2": {"name": "branch name2", "value": 456}}]}
    

    Then just grab the elements of the array and use them like branches above.

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

Sidebar

Related Questions

My JSON response looks like this : {sample:[{id:2,name:branch name},{id:3,name:branch name 2}]} My function looks
I've got the JSON coming back like this: { responseData: [{ responseData: { translatedText:
I have a simple class in asp.net mvc that looks like this: public class
I am using jQuery's getJSON function to make a request and handle the JSON
I am writing a JSON response from a perl/cgi program. The header's content type
I'm trying to generate a JSON response that includes some HTML. Thus, I have
I am not sure what is wrong with my JSON response from WCF but
[I have some code to create a JSON array. In this code I am
I'm using jQuery's form plugin (as suggested in a number of threads like this
What is the correct way to convert ASP.NET SOAP-based web services to JSON-based responses?

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.