0

I am trying to append some values by comma separated in javascript. I have this query

select boxes.dispatch_id,projects.project,boxes.hardware,sum(boxnumber) as boxnumber ,dispatches.dispatched,projects.delivery_address from dispatches,boxes,projects where 
dispatches.modified > '2017-12-20' and dispatches.id = boxes.dispatch_id and boxes.project=projects.project group by dispatches.id,boxes.hardware,boxes.dispatch_id,dispatches.dispatched  
,projects.project,projects.delivery_address order by dispatches.id

Which output some values. Now I am trying to display the data in tabular format. For each dispatch id I will have different projects and box count. My query is executing fine. So I have an array and object which holds the data with respect to dispatch id. In an array if the dispatch_id exists then I will append the values, if not then I will create another object and push the values in array.
But my program is behaving very strange way. It's always printing the last values of dispatch_id.

   var dispatchRecords = [];
        var obj = result.results;
        for(var i=0; i<obj.length; i++){
          var dispatch = {};
          var dispatch_id = obj[i][0];
          if(dispatchRecords[dispatchRecords.length-1].dispatch_id == dispatch_id){
            var j = dispatchRecords.length-1;
            dispatchRecords[j].project = dispatchRecords[j].project+", "+obj[i][1];
            dispatchRecords[j].hardware = dispatchRecords[j].hardware+", "+obj[i][2];
            dispatchRecords[j].boxnumber = dispatchRecords[j].boxnumber + obj[i][3];
          }else{
            dispatch.dispatch_id=obj[i][0];
            dispatch.project=obj[i][1];
            dispatch.hardware=obj[i][2];
            dispatch.boxnumber=obj[i][3];
            dispatch.delivery_address=obj[i][5];
            dispatchRecords.push(dispatch);
          }
        }

where as it should always display the top to bottom values with unique dispatch id and rest values should be comma separated.
I tried console my values before pushing it to the array it's fine but when I am trying to console the array it's only printing the last values. NOTE: The query is sorted with dispatch id

Output

[[6013,"001456_N",true,155,"address"],[6013,"001460_N",false,445,"address"],[6013,"001456_N",false,1394,"address"],[6013,"001436_N",true,42,"address"],[6013,"001460_N",true,28,"address"],[6013,"001436_N",false,1557,"address"],[6014,"001537_N",true,13,"address"],[6015,"001613_N",true,21,"address"],[6016,"001613_N",true,24,"address"],[6017,"001483_A",false,10,"address"],[6017,"001483_A",true,1,"address"],[6018,"001626_N",false,1,"address"],[6019,"001662_N",true,1,"address"],[6020,"001458_N",false,253,"address"]
  • 1
    Possible duplicate of [Array.push() makes all elements the same when pushing an object](https://stackoverflow.com/questions/10932584/array-push-makes-all-elements-the-same-when-pushing-an-object) – CBroe Feb 08 '18 at 12:16
  • Can you create a sample array like the one you get from the server @ZahidHussain? This is clearly a `JavaScript` question and the `SQL` query itself isn't that useful. – Angel Politis Feb 08 '18 at 12:38
  • @AngelPolitis I have created a simple output –  Feb 08 '18 at 12:43

1 Answers1

0

The problem is you are using the same dispatch object (i.e mutating the same dispatch object) in the loop. Therefore your references to the dispatch objects in the array refers to the same object. You should create a new object for each iteration.

Modify the code as follows, It should work,

var dispatchRecords = [];
var obj = result.results;
for(var i=0; i<obj.length; i++){
  var dispatch ={}
  var dispatch_id = obj[i][0];
  if(dispatchRecords.length>0 && dispatchRecords[dispatchRecords.length-1].dispatch_id == dispatch_id){
    var j = dispatchRecords.length-1;
    dispatchRecords[j].project = dispatchRecords[j].project+", "+obj[i][1];
    dispatchRecords[j].hardware = dispatchRecords[j].hardware+", "+obj[i][2];
    dispatchRecords[j].boxnumber = dispatchRecords[j].boxnumber + obj[i][3];
  }else{
    dispatch.dispatch_id=obj[i][0];
    dispatch.project=obj[i][1];
    dispatch.hardware=obj[i][2];
    dispatch.boxnumber=obj[i][3];
    dispatch.delivery_address=obj[i][5];
    dispatchRecords.push(dispatch);
  }
}
console.log(dispatchRecords);
console.log(dispatchRecords.length)
Skyler
  • 656
  • 5
  • 14