1

In the following code i am trying to populate the javascript class,such that i get an object which i can stringify to json string.

       function classValue(valuearrs) {
        for (var i = 0; i < valuearrs.length; i++) {
            for (j = 0; j < valuearrs[i].length; j++) {

                this.id = valuearrs[i][j];
                this.name = valuearrs[i][j];
                this.somekey = valuearrs[i][j];
            }
        }
  }


   function CreatenestedarrObj( valuearr) {

        this.Arrayparameters = [new classValue (valuearr)];
    }

   function ArrayMethodCall() {

       var valuearr = new Array();
       valuearr[0] = "myval1";
       valuearr[1] = "myval2";
       valuearr[2] = "myval3";
       var valuearr1 = new Array();
       valuearr1[0] = "myval11";
       valuearr1[1] = "myval12";
       valuearr1[2] = "myval13";
       nestedarr = new Array();
       nestedarr[0] = valuearr;
       nestedarr[1] = valuearr1;

       var x = new CreatenestedarrObj( nestedarr);
       var strobject = JSON.stringify(x);
       alert(strobject);
   }

   </script>
  </head> <body> MethodCall: <input type="button" value="Call Method" onclick=" ArrayMethodCall ()" />

After Stringifying the value expected is {ArrayParameters:[{myval1,myval2,myval3},{myval1,myval12,myval13}]}.The thing i can think i am missing is creating new object each time in the loop but how ? or i might be totally wrong.Any help will be much appreciated.

3 Answers 3

4

try this

    function classValue(valuearrs) {
        var arr = []
        for (var i = 0; i < valuearrs.length; i++) {
            for (j = 0; j < valuearrs[i].length; j++) {
                var temp = new Object();
                temp.id = valuearrs[i][j];
                temp.name = valuearrs[i][j];
                temp.somekey = valuearrs[i][j];
                arr[j] = temp;
            }
        }
        return arr;
  }
0
2

Change this:

function classValue(valuearrs) {
        var arr=[]
          for (var i = 0; i < valuearrs.length; i++) {
              arr[i]={};
            for (j = 0; j < valuearrs[i].length; j++) {

                arr[i].id = valuearrs[i][j];
                arr[i].name = valuearrs[i][j];
                arr[i].somekey = valuearrs[i][j];
            }
        }
          return arr;
  }
0
0

i don't know why you need to split the nestedarr to id,name and somekey but if you don't need to, just return the valuearrs and you will be fine:

function classValue(valuearrs) {
    return valuearrs;
  }


   function CreatenestedarrObj( valuearr) {

        this.Arrayparameters = [new classValue (valuearr)];
    }

   function ArrayMethodCall() {

       var valuearr = new Array();
       valuearr[0] = "myval1";
       valuearr[1] = "myval2";
       valuearr[2] = "myval3";
       var valuearr1 = new Array();
       valuearr1[0] = "myval11";
       valuearr1[1] = "myval12";
       valuearr1[2] = "myval13";
       nestedarr = new Array();
       nestedarr[0] = valuearr;
       nestedarr[1] = valuearr1;

       var x = new CreatenestedarrObj( nestedarr);
       var strobject = JSON.stringify(x);
       console.info(strobject);
   }

the output is

{"Arrayparameters":[[["myval1","myval2","myval3"],["myval11","myval12","myval13"]]]}  

or far better if you don't want it as Arrayparameters, you can use following code

var strobject = JSON.stringify(nestedarr);

and the output will be like this :

[["myval1","myval2","myval3"],["myval11","myval12","myval13"]]
1
  • Got the answer down.Syntactically In Json String array objects elements will be in {} .Thanks for replying. Commented Apr 30, 2014 at 4:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.