Make Members of JSON object accessable through a dynamic variable name

Posted on 2012-09-10
Last Modified: 2012-09-11

I am new at JS programing, but have experience in C#, ASP.NET.

What i have is a JSON object, lets call it parts.  It is parsed and ready to go.

Now in that object I have 2 members - part1 and part2

now I want to do the equivelent of below (I know the sytax is incorrect, just consider it pseudocode.

for(var i = 1; i <= 2; i++){
      parts.("part" +i) = "HELLO" + i;

Open in new window

With the desired output of parts.part1 being "Hello1 and the desired output of parts.part2 being "Hello2"

The main problem is how to be able to use the for loop to update the member of the JSON object where the datamember name needs to be dynamicly updated?

Any help is appreciated.
Question by:paulpp
    LVL 49

    Accepted Solution

    Is this what you want?
    for(var i=1; i<=2;i++) {
      parts['part'+i] = "Hullo " + parts['part'+i] + i;

    Open in new window

    LVL 19

    Expert Comment

    To do what you are asking for you need to use an array inside your JSON object.

    Your JSON object now should be something like this:

    { "parts1" : "Hello1", "parts2" : "Hello2" }

    what you actually need is:

    { "parts" : [ "Hello1", "Hello2" ] }

    This way you can access your different parts through:

    for (var x = 0; x <; x++)

    Simply, instead of using a different parameter with a number inside your data class use an array to differenciate elements of the same type.
    LVL 2

    Author Closing Comment

    Perfect - thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
    Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
    In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now