0

I'm using an ajax call through javascript and returning json.

enter image description here

I'm accessing the data using bracket notation because the object name had spaces, so I couldn't use dot notation.

This is the success function of my ajax call(not putting in the whole ajax call because of the API key).

success: function(data){
     console.log(data); 
     console.log(data['Time Series (1min)']);
},

I want the last property in the long list of properties in the "Time Series (1min)" object. I can't call it by key/property name as every minute, the property name changes (the data is minute-by-minute). I haven't found anything so far to help me online. I've tried .last() but dot notation and brackets don't seem to jive. Any ideas?

Jonas Wilms
  • 106,571
  • 13
  • 98
  • 120
J.G.Sable
  • 1,018
  • 1
  • 12
  • 31

2 Answers2

3

Once you got the data:

const series = data['Time Series (1min)'];

Just take all the keys and get the one with the highest timestamp:

const last = Object.keys(series).reduce((a, b) => a > b ? a : b);

Now that weve got the highest key, its easy:

console.log(series[last]);

All that is necessary cause object key order is not guaranteed, so you may switch over to using an array or a Map.

Jonas Wilms
  • 106,571
  • 13
  • 98
  • 120
  • Tried this and returned an error...script.js:32 Uncaught TypeError: Cannot convert undefined or null to object at Function.keys () at Object.success (script.js:32) at u (jquery.min.js:2) at Object.fireWith [as resolveWith] (jquery.min.js:2) at k (jquery.min.js:2) at XMLHttpRequest. (jquery.min.js:2) – J.G.Sable Mar 07 '18 at 21:41
  • 1
    @J.G.Sable It's `Object.keys` not `Function.keys`? – Bergi Mar 07 '18 at 21:43
  • @JonasW. You can drop the `new Date` call, ISO-formatted dates can be compared as strings directly. – Bergi Mar 07 '18 at 21:44
  • I put it after the initial console log of the entire data response. I updated the code in the original post to reflect the "answer" code that I returned an error on. – J.G.Sable Mar 07 '18 at 21:44
  • @j.g.sable that problem is not related to my code. Try logging `console.log(series)` and see what you get. And please dont change your question that confuses further readers... – Jonas Wilms Mar 07 '18 at 21:48
  • With this code: const series = data['Time Series (1min)']; console.log(series); it returns invalid API call and undefined. – J.G.Sable Mar 07 '18 at 21:50
0

I assume that you simply want to get value of the last property of the object. (Based on this topic, object properties are sorted)

What about simpler:

data[Object.keys(data).pop()]

//Edit:

First of all you want to get "Time Series" property (which changes minute by minute), so maybe you want something like this:

data[Object.keys(data).find(key => key.match(/Time Series \(\d+min\)/))]

This will get value of time zone property in your scheme (object with dates). And - as I see - data that you receive is sorted by datetime, you can get object you are interested in by running code I've written in not edited post.

Piotr Szyma
  • 3
  • 1
  • 2
  • 1
    I think the main problem is that key order is not always guaranteed. This will give you the last enumerated key of an object, but not necessarily the last timestep. You'll have to iterate all keys if you want to do that. – zero298 Mar 07 '18 at 21:50