I am experimenting with node.js, currently I am trying to visualize exchange rate developement, I got data from an API and now I am trying to sort them by date.
The data I get in the response look like this:
{
"rates": {
"2001-03-07": {
"CZK": 34.888
},
"2018-10-09": {
"CZK": 25.805
},
"2002-08-15": {
"CZK": 31.307
},
"2011-12-07": {
"CZK": 25.328
}
},
"start_at": "1999-01-01",
"base": "EUR",
"end_at": "2020-05-21"
}
Could anyone please explain to me what does this: "2001-03-07": {"CZK": 34.888}
mean? I understand that "2001-03-07"
is a key, but I am not sure about {"CZK": 34.888}
, I understand it is an another object which is just a value to the "2001-03-07"
key. Is this correct?
If so, how do I sort with this?
I would like to get the data in the following order:
"rates": {
"2001-03-07": {
"CZK": 34.888
},
"2002-08-15": {
"CZK": 31.307
},
"2011-12-07": {
"CZK": 25.328
},
"2018-10-09": {
"CZK": 25.805
}
}
I found a lot of information how to sort by value and few about sorting by key, but nothing really worked for me, is this even possible? If so how could one do this?
This is my current code:
const http = require('https');
var options = {
host: 'api.exchangeratesapi.io',
path: '/history?start_at=1999-01-01&end_at=2020-05-21&base=EUR&symbols=CZK'
};
callback = function(response) {
var str = '';
var fullData = '';
var filteredData = [];
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
fullData = JSON.parse(str);
filteredData.push(fullData.rates);
function custom_sort(a, b) {
return new Date(a.rates).getTime() - new Date(b.rates).getTime();
}
let testing = filteredData.sort(custom_sort);
console.log(testing);
});
}
http.request(options, callback).end();
Thanks