简体   繁体   English

如何将 append 数据导入 JSON 文件 NodeJS

[英]How to append data into JSON file NodeJS

I have a very large JSON file around 5 million objects我有一个非常大的 JSON 文件,大约有 500 万个对象

eg data.json例如数据.json

[
{id:1},
{id:2},
{id:3},
{id:4}
]

I need to append object into data.json file file我需要将 append object 转换成 data.json 文件文件

{id:5}

so it look like this所以看起来像这样

[
{id:1},
{id:2},
{id:3},
{id:4},
{id:5}
]

issue is I can't read that file every time because it take so long to read that file then append my object into file array object then re-write the whole file问题是我每次都无法读取该文件,因为读取该文件需要很长时间然后 append 我的 object 进入文件数组 object 然后重新写入整个文件

If you want to append any object(data), simply go to that positions of an array and put that there.如果您想 append 任何对象(数据),只需将 go 放到数组的该位置并将其放在那里。 But first try to give name to that array in which you want to append, like below,但首先尝试为您想要 append 的数组命名,如下所示,

let array = [
    {id:1},
    {id:2},
    {id:3},
    {id:4}
]


 array[4] = {id:5}
 console.log(array)

you can check this on online js compiler like this one Js Compiler你可以在在线 js 编译器上检查这个,比如这个Js Compiler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM