gpt4 book ai didi

json - Nodejs : Loop not working only gives last id 5 times

转载 作者:太空宇宙 更新时间:2023-11-04 01:55:25 25 4
gpt4 key购买 nike

我是新来的,如果这不对,我深表歉意。

所以我试图循环遍历 Node 中的 json 文件。该文件包含超过 300000 个条目,我需要获取每个条目的 ID,然后使用该 ID 执行某些操作。

我的问题是我的代码只返回最后一个 ID 5 次:

const fs = require('fs');
const path = require('path');
const json = require('big-json');

const readStream = fs.createReadStream('read-data.json');
const parseStream = json.createParseStream();

parseStream.on('data', function(pojo) {

for(i in pojo){
console.log(pojo.id); //This is where i would do something with the returned ID
}



});

readStream.pipe(parseStream);

我在这里阅读了很多帖子,他们都说要做与我在这里所做的相同的事情,但无论我尝试哪个循环,它总是相同的。

有什么想法吗?非常感谢您提前提供的帮助。

最佳答案

您需要将索引传递给循环内的对象

for(i in pojo){
console.log(pojo[i].id);
}

更喜欢here

关于json - Nodejs : Loop not working only gives last id 5 times,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48167321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com