【发布时间】:2017-01-20 11:33:01
【问题描述】:
我在 js 中工作,我得到了一个数组的响应如下:
我有什么结构的数组:
[
{ test_name: 'comp 1 test 1',
program_id: 1,
},
{ test_name: 'comp 1 test 2',
program_id: 1,
},
{ test_name: 'complete 2 test 1',
program_id: 2,
}
]
但我想得到它:
[
[
{ test_name: 'comp 1 test 1',
program_id: 1,
},
{ test_name: 'comp 1 test 2',
program_id: 1,
}
],
[
{ test_name: 'complete 2 test 1',
program_id: 2,
}
]
]
我正在使用 Promises 从数据库中获取数据
var req1 = new Promise(function(resolve, reject) {
db1.serialize(function() {
var exer_names = [];
var prog_status = 1;
db1.each("SELECT prog_name, prog_status,id from programs Where prog_createdby = "+userId+" and prog_orgid = "+prog_orgid+" and prog_status = "+prog_status, function(err, row) {
exer_names.push({id : row.id, prog_name: row.prog_name});
},function(){
resolve(exer_names);
});
});
});
var req2 = new Promise(function(resolve, reject) {
db1.serialize(function() {
var test_names = [];
db1.each("SELECT * from tests Where test_createdby = "+userId+" and org_id = "+prog_orgid+"", function(err, row) {
test_names.push({test_name: row.test_name,test_alias: row.test_alias,test_createdby: row.test_createdby,sequences: row.sequences, duration: row.duration, program_id: row.prog_id, id: row.id });
},function(){
resolve(test_names);
});
});
});
Promise.all([req1,req2]).then(function(programs, progerror) {
programs[0].forEach(function(program){
// what should be there??
});
}).catch(function(err) {
console.log('Catch: ', err);
});
【问题讨论】:
-
您是否尝试过使用 group by 子句?
-
那没有好处。
标签: javascript arrays json node.js