使用 javascript/ node js 在提供的 json 文件中保存 json 对象

save json object in there provided json file using javascript/ node js

let dataObj={
 print:{
  us:{
  "2343192u4":{
     id:"01",
     name:"linux file",
     location:"System config"
    },
   "23438ufsdjh8":{
     id:"02",
     name:"windows file",
     location:"System config"
    },
   },
   uk:{
    "23438ufsdjh8":{
     id:"01",
     name:"windows",
     location:"System"
    },
  }
   
 },
 hardware:{
  ja:{
   "9058t05":{
     no:"ct-01",
     refrence:"down-tPO-01"
    }
  }
 }
};


let folderName=[{
  filepath:"print"
 },
 {
  filepath:"hardware"
 },
 {
  filepath:"stack"
 },
 {
  filepath:"backupdir"
 },
];

let cont = {
  "45434dsdfsfv": {
    name: "us",
  },
  "4wer23434": {
    name: "uk",
  },
  "6vfkn23421": {
    name: "ja",
  },
  "9dnfjkn23e": {
    name: "ru",
  },
};


Object.values(cont).map((i) =>{
for (const [key, values] of Object.entries(dataObj)) {
            for (const [localeKey, localeValues] of Object.entries(values)) {
              console.log(localeValues);
             // fs.writeFile(path.join(__dirname,key,`${i.name}.json`), JSON.stringify(localeValues, null, 4)
            }
          }
});

我正在尝试从 obj JSON 对象中获取密钥并将其与数组文件路径进行比较,以便我可以将值推送到创建的 json 文件

这里通过使用 fs 我试图创建我从数组中获得的文件夹

fs.writeFile(path.join(__dirname,file.filepath,`${cont.name}.json`));

我用 cont json 中的名称创建了文件夹和文件 并尝试像这样

print/us.json

{
   "2343192u4":{
     id:"01",
     name:"linux file",
     location:"System config"
    },
   "23438ufsdjh8":{
     id:"02",
     name:"windows file",
     location:"System config"
   }
 }

print/uk.json

{
  "23438ufsdjh8":{
     id:"01",
     name:"windows",
     location:"System"
    },
}

hardware/ja.json

{
  "9058t05":{
     no:"ct-01",
     refrence:"down-tPO-01"
    }
}

等等...

但问题是当我这样做时,我无法将数据保存到相应的 json 文件中 我在每个 json 文件

中得到相同的输出

print/us.json

{
   "2343192u4":{
     id:"01",
     name:"linux file",
     location:"System config"
    },
   "23438ufsdjh8":{
     id:"02",
     name:"windows file",
     location:"System config"
   }
 }

print/uk.json

{
   "2343192u4":{
     id:"01",
     name:"linux file",
     location:"System config"
    },
   "23438ufsdjh8":{
     id:"02",
     name:"windows file",
     location:"System config"
   }
 }

等等...

这里 print hardware stack backupdir 总是会被更改,并且还有多个文件,因为这是系统随机生成的名称,这就是为什么我必须比较和对象中的密钥并创建这个名称的目录

如何将它们推送到具有各自值的不同文件夹中

原因:

根据您遵循的代码库

Object.values(cont).map((i) =>{
for (const [key, values] of Object.entries(dataObj)) {
            for (const [localeKey, localeValues] of Object.entries(values)) {
              console.log(localeValues);
             // fs.writeFile(path.join(__dirname,key,`${i.name}.json`), JSON.stringify(localeValues, null, 4)
            }
          }
});

您正在遍历 cont 变量,并且在循环内迭代 dataObjvalues 对象。

问题是您没有使用 localKey 断言 i 变量(值为 cont 的项目)。由于您在文件名中使用 i 变量。对于所有 localeKey-localeValue 对,文件名将相同。

因此,根据您的代码库,执行流程如下

cont = "us"
    dataObj.hardWare.ja -> write to "/hardware/us.json"
    dataObj.print.uk -> write to "/print/us.json"
    dataObj.print.us -> write to "/print/us.json"
cont = "uk"
    dataObj.hardWare.ja -> write to "/hardware/uk.json"
    dataObj.print.uk -> write to "/print/uk.json"
    dataObj.print.us -> write to "/print/uk.json"
cont = "ja"
    dataObj.hardWare.ja -> write to "/hardware/ja.json"
    dataObj.print.uk -> write to "/print/ja.json"
    dataObj.print.us -> write to "/print/ja.json"
cont = "ru" 
    dataObj.hardWare.ja -> write to "/hardware/ru.json"
    dataObj.print.uk -> write to "/print/ru.json"
    dataObj.print.us -> write to "/print/ru.json"

(注意:由于对象键已排序,因此 hardware 将在 print 之前处理。类似地,print.uk 将在 print.us 之前处理。

现在,由于 dataObj.print.us 在最后处理,所有文件都会有 dataObj.print.us 数据。

修复: 要解决此问题,您可以执行以下操作:

Object.values(cont).map((i) =>{
    for (const [key, values] of Object.entries(dataObj)) {
        for (const [localeKey, localeValues] of Object.entries(values)) {
            if (i.name === localeKey) {
                // Create directory
                fs.mkdir(path.join(__dirname, key), { recursive: true }, (err) => {
                    if (err) throw err;
                });

                // Write data
                fs.writeFile(path.join(__dirname,key,`${i.name}.json`), JSON.stringify(localeValues, null, 4), (err) => {
                    if (err) {
                        console.error(err)
                    }
                })
            }
        }
    }
});

完整代码如下:

const path = require('path')
const fs=require('fs')

let dataObj={
    print:{
        us:{
            "2343192u4":{
                id:"01",
                name:"linux file",
                location:"System config"
            },
            "23438ufsdjh8":{
                id:"02",
                name:"windows file",
                location:"System config"
            },
        },
        uk:{
            "23438ufsdjh8":{
                id:"01",
                name:"windows",
                location:"System"
            },
        }

    },
    hardware:{
        ja:{
            "9058t05":{
                no:"ct-01",
                refrence:"down-tPO-01"
            }
        }
    }
};


let folderName=[{
    filepath:"print"
},
{
    filepath:"hardware"
},
{
    filepath:"stack"
},
{
    filepath:"backupdir"
},
];

let cont = {
    "45434dsdfsfv": {
        name: "us",
    },
    "4wer23434": {
        name: "uk",
    },
    "6vfkn23421": {
        name: "ja",
    },
    "9dnfjkn23e": {
        name: "ru",
    },
};


Object.values(cont).map((i) =>{
    for (const [key, values] of Object.entries(dataObj)) {
        for (const [localeKey, localeValues] of Object.entries(values)) {
            if (i.name === localeKey) {
                // Create directory
                fs.mkdir(path.join(__dirname, key), { recursive: true }, (err) => {
                    if (err) throw err;
                });

                // Write data
                fs.writeFile(path.join(__dirname,key,`${i.name}.json`), JSON.stringify(localeValues, null, 4), (err) => {
                    if (err) {
                        console.error(err)
                    }
                })
            }
        }
    }
});