Reputation: 79
I'd like to calculate a price change for different time periods with the following code:
var lastId = data.prices.length;
var numberOf = Number(lastId);
document.getElementById('ideir').innerHTML = data.prices[numberOf][1] - data.prices[0][1];
data.prices.length on its own gives something like 274 - a number. When I try to use it as a number iD for a json fetched data it stops working. I tried to convert it to a number just in case, but first of all it already is a number, and secondly it's also not working as a number iD. The data.prices[0][1] on its own gives the correct json value - the first price from the fetched time period.
What should I do to get the value of the last available from the fetched JSON data?
Upvotes: 0
Views: 63
Reputation: 199
Array in javascript start from 0
so the last element would have the index of length - 1
. The third line should look like this:
document.getElementById('ideir').innerHTML = data.prices[numberOf-1][1] - data.prices[0][1];
Upvotes: 1