javascript - 从 api 调用中获取特定数据
问题描述
我的js代码是
<script>
fetch('https://api.binance.com/api/v1/trades?symbol=BTCUSDT')
.then(resi => resi.json())
.then(data =>(console.log(data)));
resi.send();
</script>
从它的回应来看,一个是以下一个:
[0 … 99]
0:
id: 96874552
isBestMatch: true
isBuyerMaker: true
price: "3584.58000000"
数量: "0.00293100"
时间: 1548506554914
原型: 对象
我有100个这样的结果。但是我只能从中获取数量或价格并使用数组将每个存储到变量中吗?
解决方案
是的,您可以使用map来创建所需的新对象数组。
在你的情况下:
fetch('https://api.binance.com/api/v1/trades?symbol=BTCUSDT')
.then(resi => resi.json())
.then(data => data.map(item => Object({"qty":item.qty, "price":item.price}));
resi.send();
然后data
将包含一个包含 100 个项目的数组,qty
并且price
只有。
推荐阅读
- php - 如何将 PHP 定义的表中的值提交到 php 页面以进行进一步处理?
- docker - Docker 容器没有运行,即使 -d
- azerothcore - 运行 worldserver 时出错:“000.vmtree”丢失或指向错误版本的 vmap 文件。使用最新版本的 vmap_assembler.exe 重做 vmap
- python - Python:.pop 在同一行和不同行之间的行为不同
- java - Null Dereference - 使用 HashMap.EntrySet() 加强扫描
- android - 在服务之间创建应用内(客户端)通信的最佳实践是什么?
- mysql - MYSQL如何映射这些join结果?
- excel - 需要根据工作表单元格中的唯一 ID 运行宏
- java - 调色板中缺少 GridBagLayout
- sql - 如何进行查询以将值转换为大查询中所有分区表的列