Forum Discussion

vinodkumar_chau's avatar
vinodkumar_chau
Contributor
10 years ago

How to create Mapped object Structure in JScript

Hi All, 

 

I have below mentioned Object of Variables inside TestValues which are initially uninitialezed

var TestValues = {name: "", ID: "", BaseNo: ""};

 

 

. After reading the values from below xml

<?xml version="1.0"?>
<configurations>
<Properties>
<Property name="name1" ID="0" BaseNo="34"/>
<Property name="name2" ID="1" BaseNo="35"/>
<Property name="name3" ID="2" BaseNo="37"/>
</Properties>

</configurations>

 

 , I have to set the values agianst each variable inside TestValues
but the problem is that, there should be only 1 variable for each value. For ex when i read the name from 1st property it is name1 , for 2nd it is name2. how can i have i variable storing more than 1 value.
There is some concept of mapped object structure in Jscript but i dont know how to use. Please help if you know

  • k_de_boer03's avatar
    k_de_boer03
    10 years ago

    I only hardcoded that to give an example. 

     

    It could be something like:

     

    var name, ID, BaseNo;
    
    for(var i=0; i<totalPropertyNodes;i++){
      name = GetNameFromXml(); 
      ID = GetIDFromXml();
      BaseNo = GetBaseNoFromXml();
    }
    
    new Property(name, ID, BaseNo)

    Then it is no longer hardcoded.

     

    I am not sure how you get the values from the xml, and I still don't quite understand the result you are expecting.

  • This is not a difficult problem.  We do this all the time.  Remember that in JScript, all variables are objects.  You can use an XML DOM Document to retrieve a nodelist of all the Property nodes, and then iterate through the attributes of each node.

     

    When you're creating the sub-objects, you can create "properties" on the sub objects like this:

     

    obj[attributename1] = value;

     

     

      • k_de_boer03's avatar
        k_de_boer03
        Contributor

        Not sure if I understand you correctly, but you want something like this?:

         

        function Property(name, ID, BaseNo){
          return {
            "name" : name,
            "ID" : ID,
            "BaseNo" : BaseNo
          }
        }
        
        var TestValues = [
          new Property("name1", "0", "34"),
          new Property("name2", "1", "35"),
          new Property("name3", "2", "36")
        ]

        Now you have a single Array variable that has all the properties.

        To get the ID of property 2, you can do

         

        TestValues[1].ID