Javascript Array.push()的奇怪行为

时间:2019-01-22 04:08:47

标签: javascript arrays json

这可能是我一个愚蠢的错误,但是我的JSON对象数组表现异常。

JSON对象insertDoc具有startDateendDate字段,我正尝试在--p之间的每一天创建一个新对象

            var insertDocs = [];
            var finalDate = insertDoc.endDate;

                while (insertDoc.startDate <= finalDate) {
                    insertDoc.endDate =  insertDoc.startDate;
                    console.log('doc is ' + JSON.stringify(insertDoc));
                    insertDocs.push(insertDoc);
                    insertDoc.startDate = new Date(moment(insertDoc.startDate).add(1, 'days')); 
                }

            ...

            for (var i = 0; i < insertDocs.length; i++) {
                console.log(JSON.stringify(insertDocs[i]));
            }

这是浏览器的输出-

doc is {"description":"testtask","endDate":"2019-04-01T00:00:00.000Z","startDate":"2019-04-01T00:00:00.000Z","status":"OPEN"}
doc is {"description":"testtask","endDate":"2019-04-02T00:00:00.000Z","startDate":"2019-04-02T00:00:00.000Z","status":"OPEN"}
doc is {"description":"testtask","endDate":"2019-04-03T00:00:00.000Z","startDate":"2019-04-03T00:00:00.000Z","status":"OPEN"}
doc is {"description":"testtask","endDate":"2019-04-04T00:00:00.000Z","startDate":"2019-04-04T00:00:00.000Z","status":"OPEN"}
doc is {"description":"testtask","endDate":"2019-04-05T00:00:00.000Z","startDate":"2019-04-05T00:00:00.000Z","status":"OPEN"}

stringify的输出-同一文档打印了5次

{"description":"testtask","endDate":"2019-04-05T00:00:00.000Z","startDate":"2019-04-06T00:00:00.000Z","status":"OPEN"}

因此,似乎每个对象都是不同的并且可以正确打印,但数组似乎只是同一对象的5个。日期值也不同

我在这里想念什么吗?

0 个答案:

没有答案