当使用JSON数据填充数组时遇到不一致的情况,会在控制台中显示一个空数组。
P粉438918323
P粉438918323 2023-08-03 11:50:47
[JavaScript讨论组]

我正在尝试使用AlphaVantage API获取一些数据,并且我想将某只股票支付的所有股息存储在一个数组中。我现在只尝试保存股息,但将来我希望能将股息与特定日期关联起来。

用于检索数据的函数:

async function fetchTimeSeriesDailyAdjusted (ticker) {  //Fetch function to get the daily close and the dividends
    const apiTimeSeriesDailyAdjusted = `https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=${symbol}&apikey=${apiKey}`; //Lik of the API - update the symbol

    try {
        const response = await fetch(apiTimeSeriesDailyAdjusted);
        const data = await response.json(); 
        const historicalDividend = []; //Array of the dividends
        
        for (let date in data['Time Series (Daily)']) { //This for should make the code go through all the JSON
            historicalDividend = entry ['7. dividend amount']; //This should store the dividend while the for loop "goes"
        }

        console.log(historicalDividend); //Console log to see the dividend

        return historicalDividend; //Value that the function must return
    } catch (error) {
        console.error('Error fetching apiTimeSeriesDailyAdjusted'); //Log of the error
    }
}

这是我创建的函数,但正如我所看到的,可能你也看到了,它不起作用。

P粉438918323
P粉438918323

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号