0

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);

});
2
  • have you tried to use a group by clause? Commented Jan 20, 2017 at 11:58
  • that was not beneficial. Commented Jan 20, 2017 at 12:07

2 Answers 2

3

you can create arrays on the basis of program_id and then push those arrays into the parent array

var parent = [];
var child = [];
var another_child = [];
parent.push(child);
parent.push(another_child);
Sign up to request clarification or add additional context in comments.

8 Comments

I am getting array from database
either you can change your db shema or you'll have to traverse the response array to be able to get the desired result
you need to apply little logic on program_id inside loop, wait
instead of applying loop on whole response, you should try dividing the response in multiple arrays on the basis of program_id. Then apply loop on those arrays to create child JSON objects.
stackoverflow.com/questions/14696326/… for that you can take help from here
|
1

You can create an 2D array indexed by program_id and fill it properly with 2 nested loop

var input =  [
      {
        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,
      }
    ];
    output = input.map(x => x.program_id).filter((x,i,arr) => arr.indexOf(x) === i).map(x => [x]);
    output.forEach((x,i,arr) => {
      input.forEach(y => {
        if (y.program_id === x[0]) x.push(y);
      })
    });
    output = output.map(x => x.slice(1));
    console.log(output);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.