我正在执行一个API请求,该请求提供了以下返回的数据
Object
Meta Data: {1. Information: "Intraday (5min) open, high, low, close prices and volume", 2. Symbol: "LON:TCG", 3. Last Refreshed: "2019-06-07 09:45:00", 4. Interval: "5min", 5. Output Size: "Compact", …}
Time Series (5min):
2019-06-06 09:50:00: {1. open: "16.1150", 2. high: "16.1950", 3. low: "16.1050", 4. close: "16.1950", 5. volume: "78801"}
2019-06-06 09:55:00: {1. open: "16.1100", 2. high: "16.4050", 3. low: "16.1100", 4. close: "16.1600", 5. volume: "251467"}
在时间序列(5分钟)下,有许多数据线以日期和时间戳开头。我正在尝试获取此数据的最后一个对象。
如果我执行以下操作
console.log(data['Time Series (5min)']['2019-06-06 09:45:00']);
我得到以下回报
{1. open: "16.3889", 2. high: "16.5650", 3. low: "16.3844", 4. close: "16.4500", 5. volume: "13428632"}
,但这只能在时间过去之前起作用,因此需要更新代码。我以为我可以使用密钥号获得最后一个,但是我不确定,所以我做错了。
console.log(data['Time Series (5min)'][1]);
希望这是有道理的。
我尝试使用以下方法获取对象长度
i = Object.keys('Time Series (5min)').length;
这将返回18,这不是“时间序列”上的长度
Meta Data: {1. Information: "Intraday (5min) open, high, low, close prices and volume", 2. Symbol: "LON:TCG", 3. Last Refreshed: "2019-06-07 09:50:00", 4. Interval: "5min", 5. Output Size: "Compact", …}
Time Series (5min):
2019-06-06 09:55:00: {1. open: "16.1100", 2. high: "16.4050", 3. low: "16.1100", 4. close: "16.1600", 5. volume: "251467"}
2019-06-06 10:00:00: {1. open: "16.2658", 2. high: "16.3711", 3. low: "16.1250", 4. close: "16.3711", 5. volume: "328579"}
2019-06-06 10:05:00: {1. open: "16.2050", 2. high: "16.2050", 3. low: "16.2050", 4. close: "16.2050", 5. volume: "66070"}
2019-06-06 10:20:00: {1. open: "16.4041", 2. high: "16.4041", 3. low: "16.4041", 4. close: "16.4041", 5. volume: "0"}
2019-06-06 10:25:00: {1. open: "16.3889", 2. high: "16.5650", 3. low: "16.3844", 4. close: "16.4500", 5. volume: "13428632"}
2019-06-06 10:30:00: {1. open: "16.4500", 2. high: "16.6123", 3. low: "16.3718", 4. close: "16.3718", 5. volume: "154615"}
2019-06-06 10:35:00: {1. open: "16.3800", 2. high: "16.4142", 3. low: "16.3800", 4. close: "16.4142", 5. volume: "102698"}
2019-06-06 10:40:00: {1. open: "16.6650", 2. high: "16.6650", 3. low: "16.4000", 4. close: "16.5500", 5. volume: "192113"}
2019-06-06 10:45:00: {1. open: "16.4200", 2. high: "16.5500", 3. low: "16.4100", 4. close: "16.4100", 5. volume: "221992"}
2019-06-06 10:50:00: {1. open: "16.4350", 2. high: "16.5350", 3. low: "16.4350", 4. close: "16.4350", 5. volume: "8941"}
2019-06-06 10:55:00: {1. open: "16.4650", 2. high: "16.5110", 3. low: "16.4124", 4. close: "16.4124", 5. volume: "107250"}
2019-06-06 11:00:00: {1. open: "16.4664", 2. high: "16.7900", 3. low: "16.4664", 4. close: "16.7000", 5. volume: "190689"}
2019-06-06 11:05:00: {1. open: "16.6866", 2. high: "16.9055", 3. low: "16.6443", 4. close: "16.6750", 5. volume: "332929"}
2019-06-06 11:10:00: {1. open: "16.7350", 2. high: "16.8461", 3. low: "16.6900", 4. close: "16.6900", 5. volume: "23712"}
2019-06-06 11:15:00: {1. open: "16.8000", 2. high: "16.9400", 3. low: "16.6989", 4. close: "16.8200", 5. volume: "126571"}
2019-06-06 11:20:00: {1. open: "17.2750", 2. high: "17.2750", 3. low: "16.5286", 4. close: "16.5286", 5. volume: "1182957"}
2019-06-06 11:25:00: {1. open: "16.5100", 2. high: "16.7900", 3. low: "16.4498", 4. close: "16.4498", 5. volume: "339432"}
2019-06-06 11:30:00: {1. open: "16.4275", 2. high: "16.7450", 3. low: "16.1700", 4. close: "16.4327", 5. volume: "499674"}
2019-06-07 03:05:00: {1. open: "17.0000", 2. high: "17.2950", 3. low: "16.6316", 4. close: "16.7426", 5. volume: "211668"}
2019-06-07 03:10:00: {1. open: "17.0116", 2. high: "17.1582", 3. low: "16.6450", 4. close: "16.6500", 5. volume: "421838"}
2019-06-07 03:15:00: {1. open: "16.5600", 2. high: "16.7300", 3. low: "16.4000", 4. close: "16.7300", 5. volume: "68902"}
2019-06-07 03:20:00: {1. open: "16.4701", 2. high: "16.5900", 3. low: "16.2950", 4. close: "16.4500", 5. volume: "425421"}
2019-06-07 03:25:00: {1. open: "16.3000", 2. high: "16.6050", 3. low: "16.3000", 4. close: "16.6050", 5. volume: "79936"}
2019-06-07 03:30:00: {1. open: "16.3000", 2. high: "16.3000", 3. low: "16.3000", 4. close: "16.3000", 5. volume: "7771"}
2019-06-07 03:35:00: {1. open: "16.5134", 2. high: "16.5134", 3. low: "16.5121", 4. close: "16.5121", 5. volume: "7880"}
2019-06-07 03:40:00: {1. open: "16.6154", 2. high: "16.6250", 3. low: "16.4850", 4. close: "16.6250", 5. volume: "504343"}
2019-06-07 03:45:00: {1. open: "16.3505", 2. high: "16.3505", 3. low: "16.3400", 4. close: "16.3440", 5. volume: "110414"}
2019-06-07 03:50:00: {1. open: "16.3450", 2. high: "16.5784", 3. low: "16.3150", 4. close: "16.5000", 5. volume: "17818"}
2019-06-07 03:55:00: {1. open: "16.2200", 2. high: "16.6669", 3. low: "16.1896", 4. close: "16.6669", 5. volume: "118822"}
2019-06-07 04:00:00: {1. open: "16.6670", 2. high: "16.6670", 3. low: "16.3150", 4. close: "16.3150", 5. volume: "53000"}
2019-06-07 04:05:00: {1. open: "16.6634", 2. high: "16.6634", 3. low: "16.6400", 4. close: "16.6400", 5. volume: "15810"}
2019-06-07 04:10:00: {1. open: "16.5500", 2. high: "16.6350", 3. low: "16.5277", 4. close: "16.5277", 5. volume: "24725"}
2019-06-07 04:15:00: {1. open: "16.7035", 2. high: "16.7035", 3. low: "16.5350", 4. close: "16.5572", 5. volume: "111805"}
2019-06-07 04:20:00: {1. open: "16.6880", 2. high: "16.7050", 3. low: "16.5350", 4. close: "16.5350", 5. volume: "54743"}
2019-06-07 04:25:00: {1. open: "16.5470", 2. high: "16.6696", 3. low: "16.5300", 4. close: "16.6696", 5. volume: "29039"}
2019-06-07 04:30:00: {1. open: "16.5500", 2. high: "16.6700", 3. low: "16.5500", 4. close: "16.6700", 5. volume: "20799"}
2019-06-07 04:35:00: {1. open: "16.6700", 2. high: "16.7452", 3. low: "16.5550", 4. close: "16.7452", 5. volume: "51740"}
2019-06-07 04:40:00: {1. open: "16.6950", 2. high: "16.7250", 3. low: "16.5750", 4. close: "16.5800", 5. volume: "34060"}
2019-06-07 04:45:00: {1. open: "16.7950", 2. high: "16.7950", 3. low: "16.5850", 4. close: "16.6906", 5. volume: "138170"}
2019-06-07 04:50:00: {1. open: "16.6868", 2. high: "16.6868", 3. low: "16.6868", 4. close: "16.6868", 5. volume: "107"}
2019-06-07 04:55:00: {1. open: "16.6100", 2. high: "16.7300", 3. low: "16.6100", 4. close: "16.7300", 5. volume: "89743"}
2019-06-07 05:00:00: {1. open: "16.6795", 2. high: "16.6795", 3. low: "16.6795", 4. close: "16.6795", 5. volume: "2000"}
2019-06-07 05:05:00: {1. open: "16.7850", 2. high: "16.7900", 3. low: "16.6772", 4. close: "16.7000", 5. volume: "22901"}
2019-06-07 05:10:00: {1. open: "16.7850", 2. high: "16.7850", 3. low: "16.7850", 4. close: "16.7850", 5. volume: "13549"}
2019-06-07 05:15:00: {1. open: "16.7130", 2. high: "16.9500", 3. low: "16.6400", 4. close: "16.6400", 5. volume: "257599"}
2019-06-07 05:20:00: {1. open: "16.7900", 2. high: "16.8300", 3. low: "16.7900", 4. close: "16.8300", 5. volume: "62218"}
2019-06-07 05:25:00: {1. open: "16.8786", 2. high: "16.8786", 3. low: "16.6450", 4. close: "16.6450", 5. volume: "149143"}
2019-06-07 05:30:00: {1. open: "16.7854", 2. high: "16.7854", 3. low: "16.6050", 4. close: "16.6050", 5. volume: "28239"}
2019-06-07 05:35:00: {1. open: "16.6050", 2. high: "16.9985", 3. low: "16.5750", 4. close: "16.8500", 5. volume: "1104316"}
2019-06-07 05:40:00: {1. open: "16.8833", 2. high: "17.0000", 3. low: "16.8500", 4. close: "17.0000", 5. volume: "158694"}
2019-06-07 05:45:00: {1. open: "16.7727", 2. high: "17.0433", 3. low: "16.7400", 4. close: "17.0400", 5. volume: "61278"}
2019-06-07 05:50:00: {1. open: "16.7000", 2. high: "16.9900", 3. low: "16.6350", 4. close: "16.8000", 5. volume: "226731"}
2019-06-07 05:55:00: {1. open: "16.8800", 2. high: "16.8800", 3. low: "16.6600", 4. close: "16.6600", 5. volume: "27513"}
2019-06-07 06:00:00: {1. open: "16.6550", 2. high: "16.7000", 3. low: "16.5263", 4. close: "16.6814", 5. volume: "106893"}
2019-06-07 06:05:00: {1. open: "16.5526", 2. high: "16.5526", 3. low: "16.5526", 4. close: "16.5526", 5. volume: "96457"}
2019-06-07 06:10:00: {1. open: "16.6621", 2. high: "16.6656", 3. low: "16.4700", 4. close: "16.5350", 5. volume: "71974"}
2019-06-07 06:15:00: {1. open: "16.5450", 2. high: "16.5450", 3. low: "16.5450", 4. close: "16.5450", 5. volume: "4615"}
2019-06-07 06:20:00: {1. open: "16.5400", 2. high: "16.6650", 3. low: "16.5150", 4. close: "16.5150", 5. volume: "37085"}
2019-06-07 06:25:00: {1. open: "16.5350", 2. high: "16.5682", 3. low: "16.5150", 4. close: "16.5150", 5. volume: "13874"}
2019-06-07 06:30:00: {1. open: "16.6070", 2. high: "16.6070", 3. low: "16.4950", 4. close: "16.4950", 5. volume: "6542"}
2019-06-07 06:35:00: {1. open: "16.5079", 2. high: "16.6561", 3. low: "16.5079", 4. close: "16.6355", 5. volume: "172577"}
2019-06-07 06:40:00: {1. open: "16.5050", 2. high: "16.6750", 3. low: "16.5050", 4. close: "16.6750", 5. volume: "7602"}
2019-06-07 06:45:00: {1. open: "16.6406", 2. high: "16.7514", 3. low: "16.6406", 4. close: "16.6432", 5. volume: "32088"}
2019-06-07 06:50:00: {1. open: "16.8000", 2. high: "16.8000", 3. low: "16.8000", 4. close: "16.8000", 5. volume: "895"}
2019-06-07 06:55:00: {1. open: "16.6628", 2. high: "16.8000", 3. low: "16.6611", 4. close: "16.7500", 5. volume: "104823"}
2019-06-07 07:00:00: {1. open: "16.6611", 2. high: "16.6611", 3. low: "16.6371", 4. close: "16.6371", 5. volume: "180165"}
2019-06-07 07:05:00: {1. open: "16.5100", 2. high: "16.6000", 3. low: "16.5000", 4. close: "16.5000", 5. volume: "72155"}
2019-06-07 07:10:00: {1. open: "16.5000", 2. high: "16.5000", 3. low: "16.3600", 4. close: "16.4567", 5. volume: "189338"}
2019-06-07 07:15:00: {1. open: "16.3100", 2. high: "16.4071", 3. low: "16.2800", 4. close: "16.3835", 5. volume: "38829"}
2019-06-07 07:20:00: {1. open: "16.1350", 2. high: "16.3955", 3. low: "16.0850", 4. close: "16.3000", 5. volume: "193493"}
2019-06-07 07:25:00: {1. open: "16.3000", 2. high: "16.4400", 3. low: "16.2498", 4. close: "16.4400", 5. volume: "435749"}
2019-06-07 07:30:00: {1. open: "16.3800", 2. high: "16.3800", 3. low: "16.2150", 4. close: "16.2150", 5. volume: "256465"}
2019-06-07 07:35:00: {1. open: "16.2552", 2. high: "16.4153", 3. low: "16.2552", 4. close: "16.3400", 5. volume: "123996"}
2019-06-07 07:40:00: {1. open: "16.4625", 2. high: "16.7200", 3. low: "16.4000", 4. close: "16.6444", 5. volume: "861049"}
2019-06-07 07:45:00: {1. open: "16.5202", 2. high: "16.5950", 3. low: "16.4800", 4. close: "16.5950", 5. volume: "18495"}
2019-06-07 07:50:00: {1. open: "16.5900", 2. high: "16.5900", 3. low: "16.3050", 4. close: "16.5500", 5. volume: "86096"}
2019-06-07 07:55:00: {1. open: "16.5381", 2. high: "16.5381", 3. low: "16.2300", 4. close: "16.3476", 5. volume: "57509"}
2019-06-07 08:00:00: {1. open: "16.2500", 2. high: "16.4568", 3. low: "16.1200", 4. close: "16.1500", 5. volume: "63567"}
2019-06-07 08:05:00: {1. open: "16.4310", 2. high: "16.4310", 3. low: "16.1300", 4. close: "16.1650", 5. volume: "79478"}
2019-06-07 08:10:00: {1. open: "16.4194", 2. high: "16.4194", 3. low: "16.4194", 4. close: "16.4194", 5. volume: "6593"}
2019-06-07 08:15:00: {1. open: "16.1700", 2. high: "16.4000", 3. low: "16.1700", 4. close: "16.4000", 5. volume: "14898"}
2019-06-07 08:20:00: {1. open: "16.3950", 2. high: "16.5000", 3. low: "16.3400", 4. close: "16.3400", 5. volume: "43614"}
2019-06-07 08:25:00: {1. open: "16.3745", 2. high: "16.4060", 3. low: "16.3745", 4. close: "16.4060", 5. volume: "6278"}
2019-06-07 08:30:00: {1. open: "16.4068", 2. high: "16.4068", 3. low: "16.3218", 4. close: "16.3218", 5. volume: "20939"}
2019-06-07 08:35:00: {1. open: "16.3045", 2. high: "16.4000", 3. low: "16.3045", 4. close: "16.4000", 5. volume: "42500"}
2019-06-07 08:40:00: {1. open: "16.4000", 2. high: "16.4000", 3. low: "16.2600", 4. close: "16.2600", 5. volume: "13020"}
2019-06-07 08:45:00: {1. open: "16.3790", 2. high: "16.4000", 3. low: "16.3182", 4. close: "16.3182", 5. volume: "45965"}
2019-06-07 08:50:00: {1. open: "16.2600", 2. high: "16.2600", 3. low: "16.2500", 4. close: "16.2500", 5. volume: "25000"}
2019-06-07 08:55:00: {1. open: "16.3744", 2. high: "16.4000", 3. low: "16.2600", 4. close: "16.4000", 5. volume: "19077"}
2019-06-07 09:00:00: {1. open: "16.4014", 2. high: "16.4627", 3. low: "16.3550", 4. close: "16.3860", 5. volume: "76674"}
2019-06-07 09:05:00: {1. open: "16.4000", 2. high: "16.4000", 3. low: "16.3550", 4. close: "16.3550", 5. volume: "35144"}
2019-06-07 09:10:00: {1. open: "16.3810", 2. high: "16.3810", 3. low: "16.0000", 4. close: "16.0173", 5. volume: "1386934"}
2019-06-07 09:15:00: {1. open: "16.0900", 2. high: "16.0900", 3. low: "15.8400", 4. close: "15.9954", 5. volume: "168353"}
2019-06-07 09:20:00: {1. open: "15.9595", 2. high: "15.9595", 3. low: "15.8000", 4. close: "15.8000", 5. volume: "34500"}
2019-06-07 09:25:00: {1. open: "15.9701", 2. high: "15.9813", 3. low: "15.7050", 4. close: "15.9400", 5. volume: "106737"}
2019-06-07 09:30:00: {1. open: "15.9391", 2. high: "16.0416", 3. low: "15.9391", 4. close: "16.0416", 5. volume: "27296"}
2019-06-07 09:35:00: {1. open: "16.0400", 2. high: "16.0900", 3. low: "15.7950", 4. close: "16.0375", 5. volume: "166864"}
2019-06-07 09:40:00: {1. open: "16.1632", 2. high: "16.4000", 3. low: "16.0479", 4. close: "16.1450", 5. volume: "177885"}
2019-06-07 09:45:00: {1. open: "16.1811", 2. high: "16.1894", 3. low: "16.1554", 4. close: "16.1894", 5. volume: "42454"}
2019-06-07 09:50:00: {1. open: "16.3784", 2. high: "16.3784", 3. low: "16.2518", 4. close: "16.2518", 5. volume: "9587"}
答案 0 :(得分:0)
在metaData中,您具有“上次刷新”属性,它似乎与您的上次时间序列相对应。
解决方案将是这样
console.log(data['Time Series (5min)'][data['Last Refreshed']]);
答案 1 :(得分:0)
好的。让我们找出解决方案。
首先,data['Time Series (5min)']
包含一个字典-具有键值对的对象,这取决于您的实现。
您可以通过key
访问数据,但不能访问索引。
console.log(data['Time Series (5min)'][1]);
字典没有键1
,因为您没有得到结果。
此处第二个错误:
i = Object.keys('Time Series (5min)').length;
您试图计算句子Time Series (5min)
的长度。是的,有18个字母。
要计算真实钥匙数,请尝试以下操作:
i = Object.keys(data['Time Series (5min)']).length;
此外,您可以将键保存到另一个数组中,并按索引获取最后一个。
例如,您可以使用以下代码:
keys = Object.keys(data['Time Series (5min)'])
last_key = keys[keys.length - 1]
lest_metric = data['Time Series (5min)'][last_key]