我需要我的代码使用open-exchange-rates获取当前汇率,使用money.js转换货币并将返回值设置为JSON对象。但是问题在于,JSON对象保持不变,不会被修改:
arr.prices.map(function(object) {
// get current rate
oxr.latest(function() {
fx.base = "USD";
fx.rates = oxr.rates;
// convert usd to zmw
let zmw = fx.convert(object["total"], { from: "USD", to: "ZMW" });
// set new value
object["total"] = zmw;
});
});
尝试承诺后更新: 仍然无法正常工作。
arr.prices.map(function(object) {
let p = new Promise((resolve, reject) => {
oxr.latest(function() {
fx.base = "USD";
fx.rates = oxr.rates;
let zmw = fx.convert(object["total"], { from: "USD", to: "ZMW" });
if (zmw === undefined) {
reject('Fail')
} else {
resolve(zmw);
}
});
})
p.then((zmw)=>{
object["total"] = zmw;
}).catch((error) => {
console.log('Fail')
})
});