-1

JSON ファイルを読み取るためのアドレスに変数を挿入する方法。このようなものになるはずです。しかし、これはうまくいきません。最初の部分は機能しますが、それはどのように機能するかのサンプルです。作業部分は次のように呼ばれworkingMarketCapます。

<!DOCTYPE html>
<html>
    <body>
    </body>

    <!--============================ SCRIPT ============================ -->
    <script>
        //Variables
        var myRequest = new XMLHttpRequest(); 
        var watchList = ["AAPL", "FB", "GOOGL", "MSFT"];
        var urlList = watchList.join(',');

        //IEX API requests open and send
        myRequest.open ('GET', `https://api.iextrading.com/1.0/stock/market/batch?symbols=${urlList}&types=quote`) //use API function "GET" to recieve data
        myRequest.send()

        //On the load of the data
        myRequest.onload = function(){
            var IEXData = JSON.parse(myRequest.responseText); //var IEXData converts JSON response to readable data for this code
            readData(IEXData, watchList); //output it into this HTML file         
        }

        /*
        =======================
            FUNCTIONS
        =======================
        */
        function readData(data, list) {
            for(var i = 0; i < 3; i++) {
                var workingMarketCap = data.AAPL.quote.marketCap;

                console.log(workingMarketCap);

                /* Does not work VVV */
                var marketCap = data.`${list[i]}`.quote.marketCap;

                console.log(marketCap);
            }
        }
    </script>
</html>
4

1 に答える 1