javascript - 遍历字典 - JavaScript
问题描述
我这里有一个脚本,我从 api 获取信息。
fetch(url,opts)
.then((response) => {
return response.json();
})
.then((data) => {
const newdata = data;
const balances = newdata['data']['ethereum']['address'][0];
const symbol = balances['balances'][0]['currency'];
const value = balances['balances'][0]['value'];
const bal = balances;
console.log(symbol, value);
//document.getElementById("tokenBalance").innerHTML = symbol.symbol + "," + symbol.address + "," + value;
console.log(bal);
});
console.log(bal) 给出这个输出 https://ibb.co/DRS5vNv
我如何从 balances(bal) 访问:货币 {symbol & address} 和 {value}?而不是以这样的表格格式打印出来:TokenName,Tokenaddress,balance?
因为数组的大小可以改变,在我的例子中它有 34 个元素,但它可以有 5 个 10 或 100
{balances: Array(34)}
balances: Array(34)
0:
currency: {symbol: "TOAD", address: "0x463e737d8f740395abf44f7aac2d9531d8d539e9"}
value: 0.00084138
__proto__: Object
1: {currency: {…}, value: 0.9}
2: {currency: {…}, value: 0}
3: {currency: {…}, value: 0}
4: {currency: {…}, value: 1395658765.4499083}
5: {currency: {…}, value: 0}
6: {currency: {…}, value: 0.06632153}
7: {currency: {…}, value: 0}
8: {currency: {…}, value: 0}
9: {currency: {…}, value: 0}
10: {currency: {…}, value: 391.66319725}
11: {currency: {…}, value: 22989070.87819258}
12: {currency: {…}, value: 207933.87278968}
13: {currency: {…}, value: 1}
14: {currency: {…}, value: 0}
15: {currency: {…}, value: 0}
16: {currency: {…}, value: 19304224871.809647}
17: {currency: {…}, value: 1360077933.9251838}
18: {currency: {…}, value: 100}
19: {currency: {…}, value: 1.1219391}
20: {currency: {…}, value: 3e-8}
21: {currency: {…}, value: 0}
22: {currency: {…}, value: 2461419000}
23: {currency: {…}, value: 0}
24: {currency: {…}, value: 529155969901.41156}
25: {currency: {…}, value: 0.10935729}
26: {currency: {…}, value: 4285038710.5060315}
27: {currency: {…}, value: 0}
28: {currency: {…}, value: 0}
29: {currency: {…}, value: 10}
30: {currency: {…}, value: 205.5986161}
31: {currency: {…}, value: 10}
32: {currency: {…}, value: 0.1637196}
33: {currency: {…}, value: 13367128.75739117}
length: 34
解决方案
您将返回一个JavaScript Array。使用forEach或简单的 for for 循环对其进行迭代。
在您的情况下,类似于:
bal.forEach(function(item, index, array) {
console.log(item.currency, item.value);
});
应该做的伎俩。
推荐阅读
- c# - 将 Service Fabric 应用迁移到 Azure Functions
- javascript - ng-model 未绑定到 ng-repeat 中的输入数据
- javascript - 我想在一个旁边创建两个轮播
- pandas - pandas 仅为另一个表的公共行设置列值
- c# - 我可以替换的值
文档标签? - ssl - Azure 应用服务:Windows Embedded Compact 7 上的 SSL/TLS
- excel - 在 Excel 中打开 Outlook 模板时删除签名
- python - 我希望输出类似于 [[1,2,3,4,5,6,7,8,9,10],[2,4,6,8,10,12...18,20] ,[3,6,9...27,30].....[9,18,27..90]]
- python-3.x - .arange 命令上的 Numpy 错误
- javascript - Chrome Resize 事件和 getComputedStyle