api - Vue Axios API 对象到对象数组
问题描述
我目前在我的网站上显示货币数据时遇到问题,我想在其中显示货币的简称以及货币的价值。
我正在使用这个 api,以美元为基础:https ://api.exchangeratesapi.io/latest?base=USD
我运行并获取数据的代码是:
getCurrencies() {
axios
.get("https://api.exchangeratesapi.io/latest?base=USD")
.then(response => {
this.currencies = response.data.rates;
console.log(this.currencies);
});
}
我得到的输出当然只是货币的价值,我正在寻找一种将这些对象分解为对象数组的方法,我可以在其中调用 ex:currency.short 和 currency.price
这样,我将在网站上分别显示短名称和价格,而不是在使用“response.data.rates”时仅显示货币价值
解决方案
您可以使用该Object.keys
方法迭代速率和map
函数来创建您的对象,就像这样:
this.currencies = Object.keys(response.data.rates).map(k => ({
short: k,
price: response.data.rates[k]
}))
推荐阅读
- json - 将 JSON 解组为具有多个嵌入式结构和重叠字段名称的结构
- java - 按大小排列的编号表
- python - 如何调用函数中的某个参数?
- java - Java 8 流。如何将每个枚举类型的 BigDecimal 总计聚合到自定义 bean
- javascript - 循环遍历所有不起作用的 DOM 元素
- sql - Teradata SQL - 如何将小时数添加到整数?
- reactjs - 查找后将对象合并到一个数组中
- java - 如何使用 Junit 创建可共享的测试包?
- mysql - 在 MySQL 中创建 SQL 表时遇到错误
- javascript - 尝试从 HTML 元素中的 api 数据格式化文本