简体   繁体   中英

How to add text into already existing json array?

I have a nodejs app and I need help with a command. When the user runs the command they supply it with an id and it adds that id to a json array.

I have tried everything

{
"blist": ["1038494838","83857393984", and so on ]
}
const blacklist = require('./blist.json')

I got no errors from all the things I tried. It just didn't add anything to the file.

You can use .push() for this

blist.push(userInput) //will add the user's input at the end of the array

And to write it in the object

var json = JSON.stringify(blist);

And to actually write inside of it I reccomend fs

const fs = require('fs');
fs.writeFile('blist.json', json, 'utf8', callback);

And that would be it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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