只有最后一个值在数组中重复

编程入门 行业动态 更新时间:2024-10-08 00:31:48
本文介绍了只有最后一个值在数组中重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图在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)

更多推荐

只有最后一个值在数组中重复

本文发布于:2023-11-13 03:00:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583241.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!