0
我想創建一個JSON對象,最終的結構應該是這個樣子:數據推到一個複雜的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" : {} };
然後我試着把重複循環內從數據庫中收集的數據:
//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
});
}
});
但是,這是行不通的,我不斷收到一個錯誤,指出crossdata.reports.push
不是一個函數。我想這與數組與對象,方括號和解析有關,但應用來自類似討論的解決方案對我並不適用。
關於如何實現這種結構的任何想法?
謝謝!