let obj={
print:{
"2343192u4":{
id:"01",
name:"linux file",
location:"System config"
},
"23438ufsdjh8":{
id:"02",
name:"windows file",
location:"System config"
}
},
hardware:{
"9058t05":{
no:"ct-01",
refrence:"down-tPO-01"
}
},
stack:{
"345t5fdfve":{
option1:"prefer first lost",
failure:"run backup"
}
},
backupdir:{
"cjksder8982w":{
files:"config file.json",
execute:"run after failure"
}
}
};
let Array=[{
filepath:"print"
},
{
filepath:"hardware"
},
{
filepath:"stack"
},
{
filepath:"backupdir"
},
];
Array.map((file)=>{
//console.log(file.filepath)
Object.keys(obj).forEach((key)=>{
if(key===file.filepath){
// fs.writeFile(path.join(__dirname,file.filepath,"system.json"), JSON.stringify(Object.values(obj)[0], null, 4))
console.log("yes");
}
})
});
I am trying to fetch key from the obj JSON object and compare it with the Array filepath so I can push the value in created json file
here by using fs I am trying to create folder which I got from the array
fs.writeFile(path.join(__dirname,file.filepath,"system.json"));
by which I created folder and file system.json in it and I am trying to compare the key from the obj json and filepath from the array and trying to put like this
print/system.json
{
"2343192u4":{
id:"01",
name:"linux file",
location:"System config"
},
"23438ufsdjh8":{
id:"02",
name:"windows file",
location:"System config"
}
}
hardware/system.json
{
"9058t05":{
no:"ct-01",
refrence:"down-tPO-01"
}
}
and so on ...
but the problem is when I do this
JSON.stringify(Object.values(obj)[0], null, 4)
I am getting same output in every file
print/system.json
{
"2343192u4":{
id:"01",
name:"linux file",
location:"System config"
},
"23438ufsdjh8":{
id:"02",
name:"windows file",
location:"System config"
}
}
hardware/system.json
{
"2343192u4":{
id:"01",
name:"linux file",
location:"System config"
},
"23438ufsdjh8":{
id:"02",
name:"windows file",
location:"System config"
}
}
and so on ...
here print hardware stack backupdir always get changed and there are multiple more files as this is system random generated name thats why I have to compare and the key from object and make a directory of this name
how can I push them in different different folder with there respective value
uj5u.com熱心網友回復:
嘗試改變
fs.writeFileSync(path.join(__dirname,file.filepath,"system.json"),
JSON.stringify(Object.values(obj)[0], null, 4))
(僅查看?? 中的第一個屬性值obj)到
fs.writeFileSync(path.join(__dirname,file.filepath,"system.json"),
JSON.stringify(obj[key], null, 4))
它使用從正在處理的條目中獲得obj的key值來查找屬性物件。Array
如果找到匹配的檔案路徑,使用forEachin可防止停止搜索。Object.keys(obj).forEach((key)=>{另一種選擇是使用for of在找到檔案名時中斷的回圈:
Array.forEach((file)=>{
for( let key of Object.keys(obj)) {
if(key===file.filepath) {
fs.writeFileSync(path.join(__dirname,file.filepath,"system.json"),
JSON.stringify(obj[key], null, 4));
break;
}
}
});
另外:避免使用全域建構式的名稱,例如 Array變數名稱 - 這是一個等待發生的錯誤 :-)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/426811.html
標籤:javascript node.js json nodes
下一篇:為什么將json陣列和流輸出轉換為ForEach-Object時powershellConvertFrom-Json不起作用
