For Loop写记录到文件仅打印最后一项。

huangapple go评论67阅读模式
英文:

For Loop writing records to a file prints only last item

问题

我正在尝试编写一个for循环将数据写入JSON文件,但它只打印数组中的最后一个项目。

let rawdata = fs.readFileSync('./input_data.json');
let jsondata = JSON.parse(rawdata);
let result = '';
let key = '';
let data = {
   items: []
} 
myrecords = [];
for(let i = 0; i < jsondata.records.length; i++) {
    let obj = jsondata.records[i];
    result = obj.Id;
    items = jsondata.records[i].data;
    data.items = [items];
    data.key = result;
    myrecords.push(data);
}
var log_file = fs.createWriteStream(__dirname + '/output.json', {flags: 'w'});
var log_stdout = process.stdout;
console.log = function(d) {
    log_file.write(util.format(d) + '\n');
    log_stdout.write(util.format(d) + '\n');
};
console.log(JSON.stringify(myrecords, null, 2));

请注意,上述代码有一些问题,可能不会按预期工作。如果您需要进一步的帮助,请提供更多上下文或详细信息。

英文:

I'm trying to write forloop to a json file and it only prints last item in an array

    let rawdata = fs.readFileSync(&#39;./input_data.json&#39;);
    let jsondata = JSON.parse(rawdata);
    let result=&#39;&#39;;
    let key=&#39;&#39;;
    let data = {
       items: []
    } 
    myrecords=[];
      for(let i = 0; i &lt; jsondata.records.length; i++) {
        let obj = jsondata.records[i];
        result = obj.Id
        items = jsondata.records[i].data
        data.items = [items];
        data.key=result
        myrecords.push(data)
      }
   var log_file = fs.createWriteStream(__dirname + &#39;/output.json&#39;, {flags : &#39;w&#39;});
   var log_stdout = process.stdout;
   console.log = function(d) {
    log_file.write(util.format(d) + &#39;\n&#39;);
    log_stdout.write(util.format(d) + &#39;\n&#39;);
   };
   console.log(JSON.stringify(myrecords, null, 2))

答案1

得分: 1

你在循环中一再使用相同的 data 对象,并将其引用推送到 myrecords 中。
因此,myrecords 中的所有元素引用相同的 data 对象。在循环中修改对象会影响所有先前添加的项。

你应该在循环的每次迭代中创建一个新的 data 对象。

尝试以下代码:

let rawdata = fs.readFileSync('./input_data.json');
let jsondata = JSON.parse(rawdata);
let myrecords = [];

for (let i = 0; i < jsondata.records.length; i++) {
  let obj = jsondata.records[i];
  let result = obj.Id;
  let items = jsondata.records[i].data;
  let data = {
    items: [items],
    key: result
  };
  myrecords.push(data);
}

var log_file = fs.createWriteStream(__dirname + '/output.json', { flags: 'w' });
var log_stdout = process.stdout;
console.log = function (d) {
  log_file.write(util.format(d) + '\n');
  log_stdout.write(util.format(d) + '\n');
};
console.log(JSON.stringify(myrecords, null, 2));
英文:

You are using the same data object again and again in the loop and the reference you are pushing into myrecords.
Hence, all the elements in myrecords references the same data object. When modifying the object in the loop, it affects all the previously added items.

You should create a new data object in each iteration of the loop.

Try the following:

let rawdata = fs.readFileSync(&#39;./input_data.json&#39;);
let jsondata = JSON.parse(rawdata);
let myrecords = [];

for (let i = 0; i &lt; jsondata.records.length; i++) {
  let obj = jsondata.records[i];
  let result = obj.Id;
  let items = jsondata.records[i].data;
  let data = {
    items: [items],
    key: result
  };
  myrecords.push(data);
}

var log_file = fs.createWriteStream(__dirname + &#39;/output.json&#39;, { flags: &#39;w&#39; });
var log_stdout = process.stdout;
console.log = function (d) {
  log_file.write(util.format(d) + &#39;\n&#39;);
  log_stdout.write(util.format(d) + &#39;\n&#39;);
};
console.log(JSON.stringify(myrecords, null, 2));

huangapple
  • 本文由 发表于 2023年5月28日 11:55:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定