[英]How to add text into already existing json array?
我有一個nodejs應用程序,我需要一個命令的幫助。 當用戶運行命令時,他們為它提供一個id,並將該id添加到json數組中。
我嘗試了一切
{
"blist": ["1038494838","83857393984", and so on ]
}
const blacklist = require('./blist.json')
我嘗試過的所有事情都沒有錯誤。 它只是沒有添加任何文件。
你可以使用.push()
blist.push(userInput) //will add the user's input at the end of the array
並將其寫入對象中
var json = JSON.stringify(blist);
而實際寫在里面我推薦fs
const fs = require('fs');
fs.writeFile('blist.json', json, 'utf8', callback);
就是這樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.