我有一个嵌套的对象数组,如下所示。
[
{
"region": null,
"country": null,
"territory": "Worldwide",
"territoryCode": "ALL",
"t2": null,
"t3": null,
"t4": null,
"t5": null,
"t6": null,
"t7": null,
"localLanguage": {
"territoryId": 1,
"localLanguageName": "N/A",
"localLanguageCode": null
}
},
{
"region": "Africa",
"country": "Madagascar",
"territory": null,
"territoryCode": "MG",
"t2": "AFR",
"t3": "MG",
"t4": null,
"t5": null,
"t6": null,
"t7": null,
"localLanguage": {
"territoryId": 30,
"localLanguageName": "Malagasy, French",
"localLanguageCode": "MLG, FRE"
}
},
{
"region": "Africa",
"country": null,
"territory": null,
"territoryCode": "AFR",
"t2": "AFR",
"t3": null,
"t4": …Run Code Online (Sandbox Code Playgroud) 如何在写入Node Js中的文件时将对象推入数组?我目前有以下工作代码
fs.appendFile("sample.json", JSON.stringify(data, undefined, 2) + ",");
Run Code Online (Sandbox Code Playgroud)
其中 data 是一个对象,比如
{
name:'abc',
age: 22,
city: 'LA'
}
Run Code Online (Sandbox Code Playgroud)
追加后,结果如下
{
name:'abc',
age: 22,
city: 'LA'
},
{
name:'def',
age: 25,
city: 'MI'
},
Run Code Online (Sandbox Code Playgroud)
我在这里看到 2 个问题。1.末尾的尾随逗号 2.无法形成数组并将对象推入其中
任何线索将不胜感激