0

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

Raymond_90
  • 183
  • 2
  • 12
  • JSON is a text format, like CSV or XML, so unless you're trying to work on plain text, you're actually asking how to sort arrays, or in this case, trying to sort an object. – Heretic Monkey May 21 '20 at 22:53
  • Does this answer your question? [Sort JavaScript object by key](https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key) – Heretic Monkey May 21 '20 at 22:56
  • Hi @HereticMonkey , thank you very much for your comment. I understand that JSON is a text format. I just was not really sure what "something" : { "something": 12345 } is. From you answer I understand it is a JSON object whis is a value to another key in another json object, is that correct? – Raymond_90 May 21 '20 at 23:04
  • No JSON involved. Just an object with a property that has an object value. – Heretic Monkey May 21 '20 at 23:06
  • I see, sorry I was incorrectly using JSON here, I meant JSON structure if you can call it that way. But yes, I understand now. Thanks! – Raymond_90 May 21 '20 at 23:08

1 Answers1

1

You can transfer your OBJECT into an array with a slightly different structure, but sorted in an ARRAY as seen below. You'll get a slightly different format of data to work with (this assumes date is never a key in your sub-object). You're in luck that your date keys are in a convenient format that allows easy sorting without custom comparison logic.

Sample output as array:

rates = [
  {
    "date": "2001-03-07",
    "CZK": 34.888
  },
  {
    "date": "2002-08-15",
    "CZK": 31.307
  },
  {
    "date": "2011-12-07",
    "CZK": 25.328
  },
  {
    "date": "2018-10-09",
    "CZK": 25.805
  }
]

const input = {
    "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"
};

const rates = Object.keys(input.rates).sort().map(
  date => ({date, ...input.rates[date]})
);

console.log(rates);
Chase
  • 2,833
  • 10
  • 12
  • Works nicely, I will have to spend some time experimenting with this to fully grasp the idea. Thank you very much. – Raymond_90 May 21 '20 at 23:11