There is such a problem, I can not create nested JSON, there are two nested for'a that should create JSON format,

{ index1 : {key1: val1, key2: val2}, index2 : {key1: val1, key2: val2} } 

The first pass generates a name for index, the second pass creates key and val values, how to create JSON correctly from this.

  • alert (JSON.stringify (arr)); - splash58

1 answer 1

 var tmp = {}; for ( var i=0; i < 10; i++ ){ tmp['index' + i] = {}; for ( var j=0; j < 15; j++ ){ tmp['index' + i]['key' + j] = 'val' + j; } } console.log(tmp); 

  • The problem is that my whole JSON consists of variables. and if I try to collect everything from variables, it throws out "typeerror cannot set property 0 of undefined" - pnp2000
  • one
    Well, write an example of your code, I can’t know all the details .... - Ivan Turcan
  • @ vnn198 this is not json - Jean-Claude
  • it turned out in one place was a variable in the form of an array, now everything worked - pnp2000