使用节点 js / javascript 在一个文件中存储 JSON 数据值
storing JSON data value in one file using node js / javascript
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 函数轻松地在这些文件夹中创建个人 Json 文件,fs.mkdir 我可以创建文件夹
现在我正尝试通过 systemConfig.json 存储 JSON 文件夹,但正在存储的数据仅是最后一个
它只存储相同 JSON
模式的 JSON 值
因为我的期望是这样的
期望
Dell/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",
}
}
这跟在同一个 Lenovo 文件夹之后,我在 Lenovo 文件夹中也得到了相同的值
其中有额外的 } 如何将所有 Dell 详细信息存储在一个文件中
所以,如果我正确理解了你的问题,那么你需要将写入文件的代码移到 map()
回调之外。
例如
let dataToWrite = details.map( data => {
// do what you need to do here for every element
return something
})
// write to file here
在这里,我在使用 reduce 写入文件之前格式化了数据。
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",
},
},
];
let formatted = details.reduce((acc,curr) => {
if(!acc[curr.details.systemName]){
acc[curr.details.systemName] = [];
}
acc[curr.details.systemName].push(curr.Others);
return acc;
},{})
console.log(formatted)
之后运行一个forEach通过格式化对象的keys数组
Object.keys(formatted).forEach((data) => {
let folderPath = path.join(__dirname, data);
if (!fs.existsSync(folderPath)){ //checking if folder not exists
fs.mkdir(folderPath, (err) => {
if (err) throw err;
});
}
fs.writeFile(`${folderPath}/SystemConfig.json`, JSON.stringify(formatted[data], null, 4), (err) => {
if (err) throw err;
console.log("Data added Successfully");
});
});
您将获得
格式的数据
Dell/SystemConfig.json
[
{
"OS": "linux",
"harddisk": "125GB",
"Encrypted": "yes",
"password": "abc"
},
{
"OS": "Windows",
"harddisk": "125GB",
"Encrypted": "yes",
"password": "xyz"
}
]
有效json
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 函数轻松地在这些文件夹中创建个人 Json 文件,fs.mkdir 我可以创建文件夹
现在我正尝试通过 systemConfig.json 存储 JSON 文件夹,但正在存储的数据仅是最后一个
它只存储相同 JSON
模式的 JSON 值因为我的期望是这样的
期望
Dell/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",
}
}
这跟在同一个 Lenovo 文件夹之后,我在 Lenovo 文件夹中也得到了相同的值
其中有额外的 } 如何将所有 Dell 详细信息存储在一个文件中
所以,如果我正确理解了你的问题,那么你需要将写入文件的代码移到 map()
回调之外。
例如
let dataToWrite = details.map( data => {
// do what you need to do here for every element
return something
})
// write to file here
在这里,我在使用 reduce 写入文件之前格式化了数据。
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",
},
},
];
let formatted = details.reduce((acc,curr) => {
if(!acc[curr.details.systemName]){
acc[curr.details.systemName] = [];
}
acc[curr.details.systemName].push(curr.Others);
return acc;
},{})
console.log(formatted)
之后运行一个forEach通过格式化对象的keys数组
Object.keys(formatted).forEach((data) => {
let folderPath = path.join(__dirname, data);
if (!fs.existsSync(folderPath)){ //checking if folder not exists
fs.mkdir(folderPath, (err) => {
if (err) throw err;
});
}
fs.writeFile(`${folderPath}/SystemConfig.json`, JSON.stringify(formatted[data], null, 4), (err) => {
if (err) throw err;
console.log("Data added Successfully");
});
});
您将获得
格式的数据
Dell/SystemConfig.json
[
{
"OS": "linux",
"harddisk": "125GB",
"Encrypted": "yes",
"password": "abc"
},
{
"OS": "Windows",
"harddisk": "125GB",
"Encrypted": "yes",
"password": "xyz"
}
]
有效json