刚学vue,下面这个json,我想获取exchangeTrades下的buyRate值,谢谢。
{
"code": 0,
"data": [
{
"allTrades": {
"buyQty": 0,
"buyRate": 0.5189,
"cTime": 1584957514000,
"exchange": "all",
"futureType": "all",
"id": 0,
"price": 0,
"sellQty": 0,
"sellRate": 0.4811,
"symbol": "BTC",
"timestamp": 1584957514000,
"type": "1h"
},
"exchangeTrades": [
{
"buyQty": 972958,
"buyRate": 0.5063,
"cTime": 1584957514006,
"exchange": "Huobi",
"futureType": "quarter",
"id": 0,
"price": 0,
"sellQty": 948668,
"sellRate": 0.4937,
"symbol": "BTC",
"timestamp": 1584957514006,
"type": "1h"
},
{
"buyQty": 292280,
"buyRate": 0.509,
"cTime": 1584957514016,
"exchange": "Okex",
"futureType": "swap",
"id": 0,
"price": 0,
"sellQty": 281993,
"sellRate": 0.491,
"symbol": "BTC",
"timestamp": 1584957514016,
"type": "1h"
},
{
"buyQty": 16451622,
"buyRate": 0.5318,
"cTime": 1584957514010,
"exchange": "Bitmex",
"futureType": "swap",
"id": 0,
"price": 0,
"sellQty": 14486032,
"sellRate": 0.4682,
"symbol": "XBT",
"timestamp": 1584957514010,
"type": "1h"
},
{
"buyQty": 1862092,
"buyRate": 0.5219,
"cTime": 1584957514003,
"exchange": "Okex",
"futureType": "quarter",
"id": 0,
"price": 0,
"sellQty": 1706101,
"sellRate": 0.4781,
"symbol": "BTC",
"timestamp": 1584957514003,
"type": "1h"
}
]
}
],
"dialog": {
"cancelAndClose": false,
"cancelBtn": "",
"cancelColor": "",
"code": "0",
"confirmBtn": "",
"confirmColor": "",
"content": "",
"contentColor": "",
"time": "",
"title": "OK",
"titleColor": "",
"type": "3",
"url": ""
}
}
参考GPT和自己的思路:
可以通过以下代码来获取exchangeTrades下的buyRate值:
//假设data为上述JSON数据
let buyRateArr = data.data[0].exchangeTrades.map(item => item.buyRate);
console.log(buyRateArr); //输出[0.5063, 0.509, 0.5318, 0.5219]
详解:
data.data[0]
获取到最外层的对象。exchangeTrades
是一个数组,可以使用map()
方法,遍历数组中每个对象。map()
方法的回调函数中,使用箭头函数简写,并通过item.buyRate
获取每个对象中的buyRate值。