【发布时间】:2022-02-06 03:09:34
【问题描述】:
let details = [
{
value: { tag: [] },
details: {
system: "pc",
systemName: "Dell",
ram: "4GB",
},
Others: {
OS: "linux",
harddisk: "125GB",
Encrypted: "yes",
password: "abc",
},
},
{
value: { tag: [] },
details: {
system: "pc",
systemName: "Dell",
ram: "8GB",
},
Others: {
OS: "Windows",
harddisk: "125GB",
Encrypted: "yes",
password: "xyz",
},
},
{
value: { tag: [] },
details: {
system: "laptop",
systemName: "Lenovo",
ram: "4GB",
},
Others: {
OS: "linux",
harddisk: "256GB",
Encrypted: "no",
password: null,
},
},
{
value: { tag: [] },
details: {
system: "pc",
systemName: "Lenovo",
ram: "8GB",
},
Others: {
OS: "Ubuntu",
harddisk: "125GB",
Encrypted: "yes",
password: "abc",
},
},
{
value: { tag: [] },
details: {
system: "laptop",
systemName: "hp",
ram: "4GB",
},
Others: {
OS: "linux",
harddisk: "125GB",
Encrypted: "yes",
password: "abc",
},
},
];
details.map((data) => {
let folderPath = path.join(__dirname, data.details.systemName);
fs.mkdir(folderPath, (err) => {
if (err) throw err;
});
fs.writeFile(`${folderPath}/SystemConfig.json`, JSON.stringfy(data.Others, null, 4), (err) => {
if (err) throw err;
console.log("Data added Successfully");
});
});
我正在尝试使用 systemName 创建文件夹,因此我使用了 map 函数,因此我可以借助 map 函数和 fs.mkdir 在这些文件夹中轻松创建单个 Json 文件,我可以创建文件夹
现在我正在尝试通过 systemConfig.json 存储 JSON 文件夹,但获取存储的数据只是最后一个
它只存储相同 JSON 的 schema 的 JSON 值
我的期望是这样的
期待
戴尔/SystemConfig.json
{
{
OS: "linux",
harddisk: "125GB",
Encrypted: "yes",
password: "abc",
},
{
OS: "Windows",
harddisk: "125GB",
Encrypted: "yes",
password: "xyz",
}
}
但实际上它以这种格式存储数据
{
OS: "Windows",
harddisk: "125GB",
Encrypted: "yes",
password: "xyz",
}
}
这跟随同一个联想文件夹,我在联想文件夹中也得到相同的值
其中有额外的 },我如何将所有戴尔详细信息存储在一个文件中
【问题讨论】:
-
你能在 codepen 中重现这个问题吗?
-
您的预期也是无效的 json。可能有一个包含单个对象的数组
标签: javascript node.js json nodes