JSON反序列化:齐磊的功能new Date()奇怪的行为

时间:2019-07-13 10:25:14

标签: javascript json date reviver-function

我正在尝试使用d3可视化一些数据,该数据存储在postgres DB中。当我尝试解析查询结果以获取日期数字对时,发生了奇怪的事情:
最早的日期(最早的)将始终被解析/返回为最大的+ 1日期。如果我从数据库中删除最新的观测值(日期最大),则首先解析的日期将相应降低。
通过调试等,我将其范围缩小到了reviver函数本身,“ new Date( db的字符串)”指令实际上返回了正确的日期。

console.log(xhr.responseText);

[
  {
    "reg_dt": "2019-02-17T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-02-19T23:00:00.000Z",
    "p_tot": "1"
  },
  {
    "reg_dt": "2019-02-21T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-02-24T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-02-25T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-02-27T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-02-28T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-03T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-05T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-06T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-07T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-12T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-13T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-14T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-17T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-18T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-20T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-21T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-24T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-25T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-26T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-27T23:00:00.000Z",
    "p_tot": "0"
  },
  {
    "reg_dt": "2019-03-28T23:00:00.000Z",
    "p_tot": "3"
  },
  {
    "reg_dt": "2019-04-01T22:00:00.000Z",
    "p_tot": "0"
  }
]

console.log(tmp);

(24) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {reg_dt: Wed Apr 03 2019 00:00:00 GMT+0200 (Central European Summer Time), p_tot: 0}
1: {reg_dt: Wed Feb 20 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 1}
2: {reg_dt: Fri Feb 22 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
3: {reg_dt: Mon Feb 25 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
4: {reg_dt: Tue Feb 26 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
5: {reg_dt: Thu Feb 28 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
6: {reg_dt: Fri Mar 01 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
7: {reg_dt: Mon Mar 04 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
8: {reg_dt: Wed Mar 06 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
9: {reg_dt: Thu Mar 07 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
10: {reg_dt: Fri Mar 08 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
11: {reg_dt: Wed Mar 13 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
12: {reg_dt: Thu Mar 14 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
13: {reg_dt: Fri Mar 15 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
14: {reg_dt: Mon Mar 18 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
15: {reg_dt: Tue Mar 19 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
16: {reg_dt: Thu Mar 21 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
17: {reg_dt: Fri Mar 22 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
18: {reg_dt: Mon Mar 25 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
19: {reg_dt: Tue Mar 26 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
20: {reg_dt: Wed Mar 27 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
21: {reg_dt: Thu Mar 28 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
22: {reg_dt: Fri Mar 29 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 3}
23: {reg_dt: Tue Apr 02 2019 00:00:00 GMT+0200 (Central European Summer Time), p_tot: 0}
length: 24
__proto__: Array(0)

实际代码:

        if (xhr.status === 200) {
            console.log(xhr.responseText);
            let tmp = JSON.parse(xhr.responseText, function (k, v) {
                if(k === 'reg_dt') {
                    return new Date(v);
                } else if(k === 'p_tot') {
                    return +v;
                } else {
                    return v;
                }
            });
            console.log(tmp);
...

编辑: 我更改了一些代码以不使用reviver函数,而是使用.map。仍然给出错误的结果,但是有趣的是,地图内的console.log确实返回了正确的日期:

        if (xhr.status === 200) {
            let tmp = JSON.parse(xhr.responseText)
            tmp.map(c => {
                console.log("log input string:" + c.reg_dt);
                console.log("log new Date:" + new Date(c.reg_dt));
                c.reg_dt = new Date(c.reg_dt);
                c.p_tot = +c.p_tot;
            });
            console.log(tmp);

控制台:

log input string:2019-02-17T23:00:00.000Z
log new Date:Mon Feb 18 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-02-19T23:00:00.000Z
log new Date:Wed Feb 20 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-02-21T23:00:00.000Z
log new Date:Fri Feb 22 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-02-24T23:00:00.000Z
log new Date:Mon Feb 25 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-02-25T23:00:00.000Z
log new Date:Tue Feb 26 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-02-27T23:00:00.000Z
log new Date:Thu Feb 28 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-02-28T23:00:00.000Z
log new Date:Fri Mar 01 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-03T23:00:00.000Z
log new Date:Mon Mar 04 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-05T23:00:00.000Z
log new Date:Wed Mar 06 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-06T23:00:00.000Z
log new Date:Thu Mar 07 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-07T23:00:00.000Z
log new Date:Fri Mar 08 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-12T23:00:00.000Z
log new Date:Wed Mar 13 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-13T23:00:00.000Z
log new Date:Thu Mar 14 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-14T23:00:00.000Z
log new Date:Fri Mar 15 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-17T23:00:00.000Z
log new Date:Mon Mar 18 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-18T23:00:00.000Z
log new Date:Tue Mar 19 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-20T23:00:00.000Z
log new Date:Thu Mar 21 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-21T23:00:00.000Z
log new Date:Fri Mar 22 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-24T23:00:00.000Z
log new Date:Mon Mar 25 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-25T23:00:00.000Z
log new Date:Tue Mar 26 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-26T23:00:00.000Z
log new Date:Wed Mar 27 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-27T23:00:00.000Z
log new Date:Thu Mar 28 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-03-28T23:00:00.000Z
log new Date:Fri Mar 29 2019 00:00:00 GMT+0100 (Central European Standard Time)
log input string:2019-04-01T22:00:00.000Z
log new Date:Tue Apr 02 2019 00:00:00 GMT+0200 (Central European Summer Time) 
(24) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {reg_dt: Wed Apr 03 2019 00:00:00 GMT+0200 (Central European Summer Time), p_tot: 0}
1: {reg_dt: Wed Feb 20 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 1}
2: {reg_dt: Fri Feb 22 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
3: {reg_dt: Mon Feb 25 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
4: {reg_dt: Tue Feb 26 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
5: {reg_dt: Thu Feb 28 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
6: {reg_dt: Fri Mar 01 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
7: {reg_dt: Mon Mar 04 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
8: {reg_dt: Wed Mar 06 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
9: {reg_dt: Thu Mar 07 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
10: {reg_dt: Fri Mar 08 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
11: {reg_dt: Wed Mar 13 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
12: {reg_dt: Thu Mar 14 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
13: {reg_dt: Fri Mar 15 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
14: {reg_dt: Mon Mar 18 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
15: {reg_dt: Tue Mar 19 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
16: {reg_dt: Thu Mar 21 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
17: {reg_dt: Fri Mar 22 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
18: {reg_dt: Mon Mar 25 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
19: {reg_dt: Tue Mar 26 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
20: {reg_dt: Wed Mar 27 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
21: {reg_dt: Thu Mar 28 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
22: {reg_dt: Fri Mar 29 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 3}
23: {reg_dt: Tue Apr 02 2019 00:00:00 GMT+0200 (Central European Summer Time), p_tot: 0}
length: 24
__proto__: Array(0)

EDIT2: 我更改了代码以将解析后的值收集到一个新数组中,而不是更新原始数组,并且实际上返回了正确的值。不知何故:

            let tmp = JSON.parse(xhr.responseText)
            let tmp2 = [];

            tmp.map(c => {
                tmp2.push({"reg_dt" : new Date(c.reg_dt), p_tot : +c.p_tot})
                c.reg_dt = new Date(c.reg_dt);
                c.p_tot = +c.p_tot;
            });
            console.log(tmp);console.log(tmp2);

控制台:

(24) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {reg_dt: Wed Apr 03 2019 00:00:00 GMT+0200 (Central European Summer Time), p_tot: 0}
1: {reg_dt: Wed Feb 20 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 1}
2: {reg_dt: Fri Feb 22 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
3: {reg_dt: Mon Feb 25 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
4: {reg_dt: Tue Feb 26 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
5: {reg_dt: Thu Feb 28 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
6: {reg_dt: Fri Mar 01 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
7: {reg_dt: Mon Mar 04 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
8: {reg_dt: Wed Mar 06 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
9: {reg_dt: Thu Mar 07 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
10: {reg_dt: Fri Mar 08 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
11: {reg_dt: Wed Mar 13 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
12: {reg_dt: Thu Mar 14 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
13: {reg_dt: Fri Mar 15 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
14: {reg_dt: Mon Mar 18 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
15: {reg_dt: Tue Mar 19 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
16: {reg_dt: Thu Mar 21 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
17: {reg_dt: Fri Mar 22 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
18: {reg_dt: Mon Mar 25 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
19: {reg_dt: Tue Mar 26 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
20: {reg_dt: Wed Mar 27 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
21: {reg_dt: Thu Mar 28 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 0}
22: {reg_dt: Fri Mar 29 2019 00:00:00 GMT+0100 (Central European Standard Time), p_tot: 3}
23: {reg_dt: Tue Apr 02 2019 00:00:00 GMT+0200 (Central European Summer Time), p_tot: 0}
length: 24
__proto__: Array(0)
index.js:20477 
(24) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {reg_dt: Mon Feb 18 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
1: {reg_dt: Wed Feb 20 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 1}
2: {reg_dt: Fri Feb 22 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
3: {reg_dt: Mon Feb 25 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
4: {reg_dt: Tue Feb 26 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
5: {reg_dt: Thu Feb 28 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
6: {reg_dt: Fri Mar 01 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
7: {reg_dt: Mon Mar 04 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
8: {reg_dt: Wed Mar 06 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
9: {reg_dt: Thu Mar 07 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
10: {reg_dt: Fri Mar 08 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
11: {reg_dt: Wed Mar 13 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
12: {reg_dt: Thu Mar 14 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
13: {reg_dt: Fri Mar 15 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
14: {reg_dt: Mon Mar 18 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
15: {reg_dt: Tue Mar 19 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
16: {reg_dt: Thu Mar 21 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
17: {reg_dt: Fri Mar 22 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
18: {reg_dt: Mon Mar 25 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
19: {reg_dt: Tue Mar 26 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
20: {reg_dt: Wed Mar 27 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
21: {reg_dt: Thu Mar 28 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 0}
22: {reg_dt: Fri Mar 29 2019 00:00:00 GMT+0100 (Central European Standard Time), selPl: 3}
23: {reg_dt: Tue Apr 02 2019 00:00:00 GMT+0200 (Central European Summer Time), selPl: 0}
length: 24
__proto__: Array(0)

编辑3: 进一步的代码:

let arr = [];
            let dt = tmp[0].reg_dt;
            let i = 0;

            while (dt <= tmp[tmp.length - 1].reg_dt) {
                let val = undefined;
                if (i < tmp.length && (tmp[i].reg_dt.getDate() === dt.getDate())) {
                    val = tmp[i][p_tot];
                    i++;
                }
                arr.push({"reg_dt": new Date(dt), p_tot: val});
                dt.setDate(dt.getDate() + 1);
            }

0 个答案:

没有答案