只有最后一个值在数组中重复 [英] Only the last value is being repeated in an array

查看:64
本文介绍了只有最后一个值在数组中重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在javascript中用逗号分隔一些值。我有这个查询

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

输出一些值。现在我试图以表格格式显示数据。对于每个调度ID,我将有不同的项目和盒子数。
我的查询执行正常。所以我有一个数组和对象,它保存有关dispatch id的数据。在数组中,如果dispatch_id存在,那么我将追加值,如果没有,那么我将创建另一个对象并推送数组中的值。

但我的程序表现得非常奇怪。它始终打印dispatch_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);
          }
        }

因为它应始终显示从上到下的值具有唯一的调度ID和休息值应该以逗号分隔。

我尝试控制我的值然后将其推送到数组它没关系但是当我尝试控制数组时它只打印最后的值。
注意:查询按调度ID排序

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

输出

[[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"]


推荐答案

问题是你在循环中使用相同的调度对象(即改变相同的调度对象)。因此,对数组中的dispatch对象的引用引用相同的对象。你应该为每次迭代创建一个新对象。

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)

这篇关于只有最后一个值在数组中重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆