【问题标题】:storing JSON data value in one file using node js / javascript使用节点 js / javascript 将 JSON 数据值存储在一个文件中
【发布时间】: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


【解决方案1】:

这里我在使用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)

之后通过格式化对象的keys数组运行forEach

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

【讨论】:

    【解决方案2】:

    The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

    所以,如果我正确理解了您的问题,那么您需要将写入文件的代码移到 map() 回调之外的文件中。

    例如

    let dataToWrite = details.map( data => {
        // do what you need to do here for every element
    
        return something
    })
    
    // write to file here
    
    

    【讨论】:

      猜你喜欢
      • 2019-01-11
      • 1970-01-01
      • 2016-03-05
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多