我调用 RSS 提要并使用解析器对其进行解析。我收到一个数组。我现在想最终创建一个对象,如下所示:
{
"2019-06-13": {
"rates": {
"usd": "1.1289",
"jpy": "122.44",
"gbp": "0.88948",
"chf": "1.1207"
},
"date": "2019-06-13"
}
}
该.reduce功能目前看起来像这样。当我尝试设置日期时收到错误消息:
rateJson = rateArray.reduce((acc, curr) => {
let currObj =
curr['cb:statistics'][0]['cb:exchangeRate'][0];
let currCurrency = currObj['cb:targetCurrency'][0].toLowerCase();
let currDate =
currObj['cb:observationPeriod'][0]['cb:period'][0];
let currRate = {
rate: currObj['cb:observation'][0]['cb:value'][0],
mult: currObj['cb:observation'][0]['cb:unit_mult'][0]
};
console.log(currDate,currCurrency,currRate, acc)
acc[currDate]['date'] = currDate;
acc[currDate]['rates'][currCurrency] = currRate;
return acc;
}, {});
错误信息: TypeError: Cannot set property 'date' of undefined
该console.log()语句给了我四个未定义的值,即正确的和预期的值。
MMMHUHU
相关分类