Arrays 将数据推送到复杂的JSON对象中

Arrays 将数据推送到复杂的JSON对象中,arrays,json,Arrays,Json,我想创建一个json对象,其最终结构应如下所示: crossdata = { "reports" : { {"name" : "emp1", "age" : "20", "height" : "180"}, {"name" : "emp2", "age" : "33", "height" : "185"}, {"name" : "emp3", "age" : "31", "

我想创建一个json对象,其最终结构应如下所示:

crossdata = {
     "reports" : {
                   {"name" : "emp1", "age" : "20", "height" : "180"},
                   {"name" : "emp2", "age" : "33", "height" : "185"},
                   {"name" : "emp3", "age" : "31", "height" : "176"}
                   {"name" : "emp4", "age" : "42", "height" : "188"}
                 },
     "process" : {
                   {"time" : "260", "opt" : "1", "area" : "north", "active" : "1"},
                   {"time" : "123", "opt" : "0", "area" : "north", "active" : "1"},
                 },
     "status" : {
                   {"actual" : "1", "sync" : "1"},
                   {"actual" : "1", "sync" : "0"},
                   {"actual" : "0", "sync" : "0"},
                 }
        }
如您所见,crossdata中的三个对象(或者它们是数组?)可能有不同数量的“记录”,数据是从IndexedDB收集的

因此,在最上面一行我声明:

crossdata = { "reports" : {}, "process" : {}, "status" : {} };
然后我尝试将从DB收集的数据推送到循环中:

      //some DB code (reports table) // (function(r){
        for (i=0; i<r.length; i++){
          crossdata.reports.push ({
            "name" : r[i].name, "age" : r[i].age, "height" : r[i].height
          });
        }
      });
      //some DB code (process table) // (function(r){
        for (i=0; i<r.length; i++){
          crossdata.process.push ({
            "time" : r[i].time, "opt" : r[i].opt, "area" : r[i].area, "active" : r[i].active
          });
        }
      });
      //some DB code (status table) // (function(r){
        for (i=0; i<r.length; i++){
          crossdata.status.push ({
            "actual" : r[i].actual, "sync" : r[i].sync
          });
        }
      });
//一些数据库代码(报表表)//(函数(r){
对于(i=0;i使用

声明数组。您在那里声明了对象,这些对象没有本机的
push()
方法

{}
是一个对象文字,而
[]
是一个数组文字。如果要使用像
push()
这样的数组函数,显然必须声明数组

crossdata = { "reports" : [], "process" : [], "status" : [] };