对大记录执行数据操作会导致未定义的行为

时间:2020-09-24 19:35:53

标签: javascript node.js

我有一个日期集,其中包含约35,000条记录,并且采用JSON数组格式。

现在,我将其导入到我的程序中,如下所示,

const jsonfile = require('jsonfile')
const fs = require('fs');
let rawSample = jsonfile.readFileSync("./raw_sample.json");

然后我进行迭代,执行操作并将其保存如下,

rawSample.forEach(function (element, index) {
    element.input.forEach(function (element, item) {
                if (isUnicode(element)) {
                    rawSample[index].input.splice(item, 1);
                    // Check if Input Array is Empty then remove the Index
                    if (!rawSample[index].input.length) {
                        rawSample.splice(index, 1);
                        return;
                    } else {
                        return;
                    }
                }
    })
})

jsonfile.writeFileSync("./test-sample.json", rawSample);

现在,发生的情况是,test-sample.json上仍然有带有Unicode字符的元素,但是如果我通过将raw sample自身设置为test-sample.json来运行上面的代码,它将开始删除剩余的项目,但是我必须尝试5-6次以完全清除所有内容。

我不确定为什么会这样。我已经尽力调试了一切,但到目前为止还没有运气。

0 个答案:

没有答案