英文:
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('./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))
答案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('./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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论