希希莱娜 2019-10-10 10:29 采纳率: 64.3%
浏览 525
已采纳

数组内的值与显示的不一样

我遍历数组,让数组内两两相加后log出来的数组显示对的,但是每个index对应的值不一样,这是为什么
原数组(73) 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 60, 85, 125, 0, 0, 40, 110, 0, 70, 110, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110]
遍历后数组(38) [0, 0, 0, 0, 0, 0, 0, 40, 145, 125, 40, 110, 180, 0, 0, 0, 0, 0, 30, 0, 0, 0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 0, ob: Observer]

0: 0
1: 0
2: 0
3: 0
4: 0
5: 0
6: 0
7: 0
8: 40
9: 0
10: 60
11: 0
12: 85
13: 0
14: 0
15: 0
16: 0
17: 0
18: 125
19: 0
20: 0
21: 0
22: 0
23: 0
24: 40
25: 0
26: 0
27: 0
28: 0
29: 0
30: 0
31: 0
32: 0
33: 0
34: 0
35: 0
36: 110
37: 0
代码
//定义x轴数组 
            const xAxisTen = [].concat(this.xAixsTimeData)
            const xAixsTenMinute = xAxisTen.filter((value,index)=>{
                if(index%2===0){
                    return true
                }
            })
            this.xAixsTotalData = xAixsTenMinute
            console.log('10 xz',this.xAixsTotalData)

            //总乘客
            const tenMinuteTotal = [].concat(this.resTotalPassenger)
            console.log('123',tenMinuteTotal)
            let resTotal = []
            let resTotalHead = tenMinuteTotal[0]
            tenMinuteTotal.filter((item,index,arr)=>{
                if(index%2 ===0){
                    let sumTotal = (arr[index+1]||0)+(arr[index+2]||0);
                    resTotal.push(sumTotal)
                }
            })
            resTotal.unshift(resTotalHead)
            this.resTotalPassenger =[]
            this.resTotalPassenger = [].concat(resTotal)
            console.log('10 total',this.resTotalPassenger)

            //出站乘客
            const tenMinuteOut = [].concat(this.resOutPassenger)
            let resOut = []
            let resOutHead = tenMinuteOut[0]
            tenMinuteOut.filter((item,index,arr)=>{
                if(index%2 ===0){
                    let sumOut = (arr[index+1]|| 0)+(arr[index+2]||0);
                    resOut.push(sumOut)
                }
            })
            resOut.unshift(resOutHead)
            this.resOutPassenger =[]
            this.resOutPassenger = [].concat(resOut)
            console.log('10 out',this.resOutPassenger)

            //定义 进入的乘客相加
            const tenMinuteIn = [].concat(this.resInPassenger)
            let resIn = []
            let resInHead = tenMinuteIn[0]
            tenMinuteIn.filter((item,index,arr)=>{
                if(index%2 ===0){
                    let sum = (arr[index+1]|| 0)+(arr[index+2]||0);
                    resIn.push(sum)
                }
            })
            resIn.unshift(resInHead)
            //console.log(this.resInPassenger)
            this.resInPassenger =[]
            this.resInPassenger = [].concat(resIn)
            console.log('10 in',this.resInPassenger)

            this.drawPassengerChart()
  • 写回答

3条回答 默认 最新

  • 一只成序源 2019-10-10 10:37
    关注

    对于顺序问题,最好使用最原始的for循环控制才能保证。迭代方式不能保证顺序一致

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?