gpt4 book ai didi

javascript - 如何循环遍历 Alpha Vantage 时间戳

转载 作者:行者123 更新时间:2023-11-30 13:58:05 25 4
gpt4 key购买 nike

我正在尝试从我的 API 调用中循环遍历 JSON,并在我的 React 应用程序中绘制每个最高交易价格(最终目标是创建一个图表)。问题是,我需要事先知道确切的时间戳字符串,因为 API 不是数字结构的。这是我的代码(请参阅控制台日志)。我目前正在创建循环之前测试正确的端点,这就是为什么你看不到一个。

如果你很好奇,this.state.stock 是 AMD,我用它来测试它。最终它将是用户输入。我应该如何循环?

componentDidMount() {
axios
.get(`https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=${this.state.stock}&interval=5min&apikey=J6ED0QFWG3T1KLTD`)
.then((response) => {
this.setState({
dailyQuote: response.data

})
console.log("daily quote",this.state.dailyQuote['Time Series (5min)']['2019-06-27 14:15:00']['2. high'])

})
}

这是 API 调用数据的示例

{
"Meta Data": {
"1. Information": "Intraday (5min) open, high, low, close prices and volume",
"2. Symbol": "amd",
"3. Last Refreshed": "2019-06-28 16:00:00",
"4. Interval": "5min",
"5. Output Size": "Compact",
"6. Time Zone": "US/Eastern"
},
"Time Series (5min)": {
"2019-06-28 16:00:00": {
"1. open": "30.3900",
"2. high": "30.4000",
"3. low": "30.3300",
"4. close": "30.3700",
"5. volume": "2242133"
},
"2019-06-28 15:55:00": {
"1. open": "30.3700",
"2. high": "30.4400",
"3. low": "30.3601",
"4. close": "30.3900",
"5. volume": "1294256"
},
"2019-06-28 15:50:00": {
"1. open": "30.4350",
"2. high": "30.4500",
"3. low": "30.3500",
"4. close": "30.3700",
"5. volume": "1265203"
},
"2019-06-28 15:45:00": {
"1. open": "30.4750",
"2. high": "30.4750",
"3. low": "30.4300",
"4. close": "30.4350",
"5. volume": "664693"
},
"2019-06-28 15:40:00": {
"1. open": "30.4850",
"2. high": "30.4900",
"3. low": "30.4550",
"4. close": "30.4700",
"5. volume": "539474"
},
"2019-06-28 15:35:00": {
"1. open": "30.4750",
"2. high": "30.5050",
"3. low": "30.4500",
"4. close": "30.4900",
"5. volume": "685410"
},
"2019-06-28 15:30:00": {
"1. open": "30.5100",
"2. high": "30.5200",
"3. low": "30.4600",
"4. close": "30.4800",
"5. volume": "376771"
},
"2019-06-28 15:25:00": {
"1. open": "30.5400",
"2. high": "30.5600",
"3. low": "30.5000",
"4. close": "30.5101",
"5. volume": "288554"
},
"2019-06-28 15:20:00": {
"1. open": "30.5600",
"2. high": "30.5600",
"3. low": "30.5200",
"4. close": "30.5350",
"5. volume": "218143"
},
"2019-06-28 15:15:00": {
"1. open": "30.5703",
"2. high": "30.5800",
"3. low": "30.5400",
"4. close": "30.5557",
"5. volume": "281558"
},
"2019-06-28 15:10:00": {
"1. open": "30.5700",
"2. high": "30.5850",
"3. low": "30.5500",
"4. close": "30.5750",
"5. volume": "290714"
},
"2019-06-28 15:05:00": {
"1. open": "30.5803",
"2. high": "30.6100",
"3. low": "30.5750",
"4. close": "30.5750",
"5. volume": "169868"
},
"2019-06-28 15:00:00": {
"1. open": "30.6050",
"2. high": "30.6100",
"3. low": "30.5800",
"4. close": "30.5850",
"5. volume": "186744"
},
"2019-06-28 14:55:00": {
"1. open": "30.5650",
"2. high": "30.6100",
"3. low": "30.5500",
"4. close": "30.6050",
"5. volume": "378489"
},
"2019-06-28 14:50:00": {
"1. open": "30.5700",
"2. high": "30.5800",
"3. low": "30.5500",
"4. close": "30.5650",
"5. volume": "247525"
},
}

最佳答案

如果你只是想遍历数据,使用这样的东西:

var strProp, strValue;
for (strProp in someArray) {
console.log(`Name: ${strProp}; Value: ${someArray[strProp}\n`;
}

这将显示名称。由于索引签名,您还可以执行类似的操作以在代码中实际使用它们:

var data = { "meta" : "", "series" : ""};
for (var strProp in apiResults) {
if (strProp == "Meta Data") {
data["meta"] = apiResults[strProp];
} else if (strProp == "Time Series (5min)") {
data["series"] = apiResults[strProp];
} else {
console.error(`Cannot read data from API call! Returns: ${apiResults}`);
}

然后您可以用相同的方式从 data["series"] 中读取数据。

关于javascript - 如何循环遍历 Alpha Vantage 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56826647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com