我有一个下面的程序,它将一个json放入一个名为Implementations的变量中,运行函数并将json与原始json合并。
var Implementations = {
"UniversalOne": "",
"CommonOne": "",
"Implementations": [
{
"BirthDate": "",
"UniqueTraits": "",
"Male": {
"Gender": "Male",
"PlaceOfBirth": "",
"Weight": "",
"Height": "",
"EyeColor": ""
},
"Female": {
"Gender": "Female",
"PlaceOfBirth": "",
"Weight": "",
"Height": "",
"EyeColor": ""
},
"Country": [
{
"Orientation": "Male",
"Name": "ABCD",
"County": "East"
},
{
"Orientation": "Male",
"Name": "ABCD",
"County": "West"
},
{
"Orientation": "Female",
"Name": "EFGH",
"County": "East"
},
{
"Orientation": "Female",
"Name": "EFGH",
"County": "West"
},
{
"Orientation": "Female",
"Name": "IJKL"
}
],
"State": [
{
"Address": "XYZ Street",
"ZipCode": "US"
}
],
"Boy": [
{
"AgeGroup": "A",
"Id": 1,
"MaternalName": "",
"PaternalName": ""
},
{
"AgeGroup": "B",
"Id": 2,
"MaternalName": "",
"PaternalName": ""
},
{
"AgeGroup": "C",
"Id": 3,
"MaternalName": "",
"PaternalName": ""
}
]
}
],
"PersonalityTraits": [
{
"Type": "Positive"
},
{
"Type": "Negative"
}
],
"UniversalTwo": "",
"CommonTwo": "",
"EatingHabits": {
"Type": "Excessive"
},
"ReadingHabits": {
"Type": "Fast"
},
"FitnessHabits": {},
"UniversalThree": "",
"CommonThree": ""
}
function modifyImplementations(Implementations) {
var finalResult = [];
for (var i = 0; i < Implementations.Implementations.length; i++) {
var currentImplementation = Implementations.Implementations[i];
var targetObj = {
"Male": {
"Gender": "Male",
"Country": [],
"State": currentImplementation.State
},
"Female": {
"Gender": "Female",
"Country": [],
"State": currentImplementation.State
}
};
for (var j = 0; j < currentImplementation.Country.length; j++) {
var currentCountry = currentImplementation.Country[j];
if (currentCountry.Orientation === 'Male') {
targetObj.Male.Country.push(currentCountry);
} else if (currentCountry.Orientation === 'Female') {
targetObj.Female.Country.push(currentCountry);
}
}
finalResult.push(targetObj);
}
return finalResult
}
var x = Object.assign({}, Implementations);
x.Implementations = modifyImplementations(Implementations);
console.log(JSON.stringify(x));我想从名为InputFolder的文件夹中运行多个json所在的文本文件,运行相同的函数modifyImplementations,并将更新后的文件写入另一个名为outputfolder的文件夹中。
var fs = require('fs')
var async = require('async'),
const path = "./InputFolder/";
fs.readdir(path, (err, files) => {
if (err) {
debugger;
console.log(err);
return;
}
files.forEach(file => {
fs.readFile(path + file, 'utf8', function (err,finalResult) {
if (err) {
return console.log(err);
}
function modifyImplementations(Implementations){
let finalResult = [];
for (let c=0; c<Implementations.Implementations.length; c++) {
var currentImplementation = Implementations.Implementations[i];
var targetObj = {
"Male": {
"Gender": "Male",
"Country": [],
"State": currentImplementation.State
},
"Female": {
"Gender": "Female",
"Country": [],
"State": currentImplementation.State
}
};
for (var j = 0; j < currentImplementation.Country.length; j++) {
var currentCountry = currentImplementation.Country[j];
if (currentCountry.Orientation === 'Male') {
targetObj.Male.Country.push(currentCountry);
} else if (currentCountry.Orientation === 'Female') {
targetObj.Female.Country.push(currentCountry);
}
}
finalResult.push(originalObject);
}
return finalResult
}
modifyImplementations(Implementations, ()=>{
fs.writeFile('./OutputFolder/' + file,finalResult,'utf8', function (err) {
if (err) {
return console.log(err);
}
});
});
});
});
});我得到一个错误,无法读取属性Implementations.length,因为现在我在变量Implementations.How中没有json,我要重构程序吗?
发布于 2019-07-27 06:41:39
您只需在每个for循环之前使用if语句验证对象属性是否为数组。
if (obj.prop instanceOf Array) {
for (var i = 0; i < obj.prop.length; i++) {
// your logic here.
}
}
https://stackoverflow.com/questions/57210966
复制相似问题