【发布时间】:2016-06-28 17:41:44
【问题描述】:
我正在循环 2 个 JSON 对象。一个包含一组国家对象,另一个是一组机场对象。如果该机场的国家/地区与当前国家/地区匹配,我想循环并返回一个结合了两者信息的模型:
const fs = require('fs');
const allData = require('./airports.json');
const countryCodes = require('./codes.json');
const newData = countryCodes.map(country => {
allData.map(airport => {
if (country.name === airport.country) {
// console.logging here shows me that my equality check is working...
return {
"airport": `${airport.code} - ${airport.name}`,
"countryName": `${airport.country}`,
"countryCode": `${country["alpha-2"]}`
}
}
})
})
fs.writeFile("./newAirlines.json", JSON.stringify(newData, null, 2), function(err) {
if (err) {
console.log(err);
}
});
但是,当我打开它写入的 newAirlines.json 文件时,我只得到一个 null、null、null 的数组...想知道这是否与在文件之前异步尝试写入文件有关有机会完成循环(?),但不确定。
感谢所有帮助。谢谢!
【问题讨论】:
标签: javascript json node.js