使用变量

时间:2017-01-29 13:40:26

标签: javascript json node.js

我的问题是我想在getSummoner函数中使用我的参数并将其应用于我的JSON打印消息查询 - profile.summonerName.name

const https = require('https');

function printMessage(username, level) {
  const message = `${username} is summoner level ${level}`;
  console.log(message);
}

function getSummoner(summonerName){
        const request = https.get('https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/${summonerName}?api_key={myAPI}', response => {
            let body = "";
            response.on('data', data => {
                body += data.toString();
            });


            response.on('end', () => {
                const profile = JSON.parse(body);
                printMessage(profile.cygnuscrossna.name, profile.cygnuscrossna.summonerLevel)
                console.log(profile)

            });

        });
}

getSummoner("cygnuscrossna")

console.log会吐出这个JSON:

{
    cygnuscrossna:{
       id: 58971790,
       name: 'CygnusCrossNA',
       profileIconId: 1301,
       revisionDate: 1474365186000,
       summonerLevel: 15
    }
}

并打印消息吐出:

  

CygnusCrossNA是召唤师等级15

但它正确地吐出来因为我做了: profile.cygnuscrossna.nameprofile.cygnuscross.summonerLevel

如何将这些与我的论据summonerName交换?

0 个答案:

没有答案