I am working in js and I got the response of one my array as follows:
What structure of array I have:
[
{ 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,
}
]
But I want to get it like:
[
[
{ 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,
}
]
]
I am getting data from database using 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);
});