So I’m trying to enumerate objects of a JSON object using a for loop. I’m using the CoinMarketCap API to get the data about the coins. I’m assigning variables to each property by using this method:
for(var propertyName in myObject) {
// propertyName is what you want
// you can get the value like this: myObject[propertyName]
}
However, whenever I try to run the code I get no errors and nothing prints. I’m not quite sure what to do. Any feedback would be appreciated.
Here is an example using only bitcoin at the JSON object:
const LIST = [
{
"status":{
"timestamp":"2022-05-27T20:20:26.826Z",
"error_code":0,
"error_message":"None",
"elapsed":46,
"credit_count":1,
"notice":"None",
"total_count":10043
},
"data":[
{
"id":1,
"name":"Bitcoin",
"symbol":"BTC",
"slug":"bitcoin",
"num_market_pairs":9471,
"date_added":"2013-04-28T00:00:00.000Z",
"tags":[
"mineable",
"pow",
"sha-256",
"store-of-value",
"state-channel",
"coinbase-ventures-portfolio",
"three-arrows-capital-portfolio",
"polychain-capital-portfolio",
"binance-labs-portfolio",
"blockchain-capital-portfolio",
"boostvc-portfolio",
"cms-holdings-portfolio",
"dcg-portfolio",
"dragonfly-capital-portfolio",
"electric-capital-portfolio",
"fabric-ventures-portfolio",
"framework-ventures-portfolio",
"galaxy-digital-portfolio",
"huobi-capital-portfolio",
"alameda-research-portfolio",
"a16z-portfolio",
"1confirmation-portfolio",
"winklevoss-capital-portfolio",
"usv-portfolio",
"placeholder-ventures-portfolio",
"pantera-capital-portfolio",
"multicoin-capital-portfolio",
"paradigm-portfolio"
],
"max_supply":21000000,
"circulating_supply":19051093,
"total_supply":19051093,
"platform":"None",
"cmc_rank":1,
"self_reported_circulating_supply":"None",
"self_reported_market_cap":"None",
"last_updated":"2022-05-27T20:20:00.000Z",
"quote":{
"USD":{
"price":28884.822938928377,
"volume_24h":36890024864.8984,
"volume_change_24h":3.7577,
"percent_change_1h":1.64134455,
"percent_change_24h":-1.58384262,
"percent_change_7d":-1.40531253,
"percent_change_30d":-26.19444835,
"percent_change_60d":-39.59610174,
"percent_change_90d":-26.44085379,
"market_cap":550287448098.0579,
"market_cap_dominance":46.0149,
"fully_diluted_market_cap":606581281717.5,
"last_updated":"2022-05-27T20:20:00.000Z"
}
}
}
]
}
];
data = LIST['data'];
for (var d in data){
var coinName = d['name']
var coinPrice = d['quote']['USD']['price'];
var hrChange = d['quote']['USD']['percent_change_1h'];
console.log(coinName);
console.log(coinPrice);
console.log(hrChange);
}