我试图使用node.js重构一个复杂的JSON数组,下面是我的代码
let n = JSON.parse(JSON.stringify(arr2));
async.forEachOf(n, function(value, key, cb1) {
let obj = [];
async.forEachOf(value.flightDetail.flightDetails, function(v, k, cb2) {
let dateTimeInfo = v.flightInformation.productDateTime;
let isNextDay = ((dateTimeInfo.dateVariation) ? true : false);
let sectorInf = v.flightInformation.location;
let flightDetails = {};
flightDetails.eType = v.flightInformation.productDetail.equipmentType;
flightDetails.marketingCarrier = v.flightInformation.companyId.marketingCarrier;
flightDetails.operatingCarrier = v.flightInformation.companyId.operatingCarrier || v.flightInformation.companyId.marketingCarrier;
obj['segment-' + ((k) + 1)] = {
departureDate: dateTimeInfo.dateOfDeparture,
arrivalDate: dateTimeInfo.dateOfArrival,
departureTime: dateTimeInfo.timeOfDeparture,
arrivalTime: dateTimeInfo.timeOfArrival,
isNextDay: isNextDay,
sourceSector: sectorInf[0].locationId,
sourceTerminal: sectorInf[0].terminal || "",
desitinationSector: sectorInf[1].locationId,
desitinationTerminal: sectorInf[1].terminal || "",
flightInfo: flightDetails
}
console.log(JSON.stringify(obj)); **-- This doesn't working**
console.log(obj); **-- this does work**
cb2();
}, function() {
arr3.push({
flight: obj,
price: value.priceObject
});
cb1();
});
}, function() {
callback();
});
当我试图输出最终的 arr3 响应由于某种原因它似乎不起作用。但是当我安装它时,它的打印正确。
与promise / async有关吗?
答案 0 :(得分:0)
导致问题的代码和平obj['segment-' + ((k) + 1)]
的问题,我不知道为什么会发生这种情况。通过将其更改为简单的返回语句,它解决了问题。
工作样本:
let n = _.map(dataArray, function(flightObjectVal) {
let flightObj = flightObjectVal.flightDetail.flightDetails;
let pricedObj = flightObjectVal.priceObject;
return {
flight: _.map(flightObj, function(v) {
// Incase of flightInformation key is missing in the response object append with one.
if (!v.flightInformation) {
v.flightInformation = v;
}
// Date & Time object
let dateTimeInfo = v.flightInformation.productDateTime;
// Is the flight next day
let isNextDay = ((dateTimeInfo.dateVariation) ? true : false);
// Flight sector object
let sectorInf = v.flightInformation.location;
// Define restructured flight detials object
let flightDetails = {};
// Flight Equipment type
flightDetails.eType = v.flightInformation.productDetail.equipmentType;
// Marketing airline code
flightDetails.marketingCarrier = v.flightInformation.companyId.marketingCarrier;
// Operating airline code
flightDetails.operatingCarrier = v.flightInformation.companyId.operatingCarrier || v.flightInformation.companyId.marketingCarrier;
//Return the final restructured object
return {
departureDate: dateTimeInfo.dateOfDeparture,
arrivalDate: dateTimeInfo.dateOfArrival,
departureTime: dateTimeInfo.timeOfDeparture,
arrivalTime: dateTimeInfo.timeOfArrival,
isNextDay: isNextDay,
sourceSector: sectorInf[0].locationId,
sourceTerminal: sectorInf[0].terminal || "",
desitinationSector: sectorInf[1].locationId,
desitinationTerminal: sectorInf[1].terminal || "",
flightInfo: flightDetails
}
}),
pricedObj: pricedObj
}
});
我使用lodash进行了一些重构,感谢marvel308指出了async.forEachOf
同步问题。这让我重新思考了整个逻辑。