I get results from a query where each field is a separate table. For exemple, for this table (ordered by Product, Step) :
PRODUCT | STEP | WORKER
Car | Polishing | Carl
Car | Painting | Peter
Car | Painting | Mark
Bike | Painting | Paul
Boat | Repairing | Alex
I'll have 3 tables (Product, Step, Worker). I'd need to put this in another object ending up like that :
var factory = [
[
[{"product":"Car","step":"polishing","workers":[{"name":"Carl"}]}],
[{"product":"Car","step":"painting","workers":[{"name":"Peter"},{"name":"Mark"}]}]
],
[
[{"product":"Bike","step":"painting","workers":[{"name":"Paul"}]}]
],
[
[{"product":"Boat","step":"repairing","workers":[{"name":"Alex"}]}]
]
]
I'm using javascript. Could anyone help please ?
Here's what I tried :
var factory = [];
var currentProduct = [];
var currentStep = [];
var currentWorker = {"name":workers[0]};
var currentStepItem = {
"product":product[0],
"step":step[0],
"workers":[currentWorker]
};
currentStep.push(currentStepItem);
currentProduct.push(currentStep);
factory.push(currentProduct);
for(var i = 1 ; i < product.length ; i++){
if(product[i] == product[i-1]){
//I'm stuck here
}
}