首页 > 解决方案 > 从 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个这样的结果。但是我只能从中获取数量或价格并使用数组将每个存储到变量中吗?

标签: javascripthtmljsonapiserver

解决方案


是的,您可以使用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只有。


推荐阅读