【发布时间】:2021-06-18 08:42:27
【问题描述】:
我正在编写一个简单的 JavaScript 货币转换器。从 API 中提取货币数据并将其存储到状态对象中。接下来,我想将货币名称、国家和符号添加到应用程序中,因此我从 Rest Country API 中提取数据并将其存储到“货币”数组中。
代码如下:
export const state = {
date: [],
time: {},
currency: [],
rates: {},
result: [],
};
export const getSymbolsCountry = async function (symbol) {
try {
const data = await fetch(`https://restcountries.eu/rest/v2/currency/${symbol}`);
const json = await data.json();
addInfoToSymbol(json[0]);
} catch (err) {
console.log(err);
}
};
function addInfoToSymbol(data) {
let country = {
code: data.currencies[0].code,
symbol: data.currencies[0].symbol,
flag: data.flag,
country: data.name,
};
state.currency.push(country);
}
从状态记录货币的控制台显示它是一个数组,但在它上面使用 map 或 forEach 什么也没做。 console log state.currency 有人可以帮我理解我做错了什么,因为我无法理解它。
【问题讨论】:
标签: javascript arrays json object