user2242044
user2242044

Reputation: 9213

getting last element in JSON not working

I am trying to take the last element in a JSON. I know order is not preserved, but the keys are unix timestamps so I can just sort. I am working from this question:

get last element of a json object in javascript

But I get nothing logged to the console and no errors.

my code:

var mydata = {"1509937402379":"7348.01","1509937412486":"7348.01","1509937422253":"7348.01","1509937426286":"7348.01","1509937430066":"7345.54"}

console.log(mydata[Object.keys(obj).sort().pop()]);

https://jsfiddle.net/Lmb5sd1m/1/

Upvotes: 0

Views: 108

Answers (1)

Dipak
Dipak

Reputation: 2308

You missed.. .You require to use Object.keys(mydata).

var mydata = {"1509937402379":"7348.01","1509937412486":"7348.01","1509937422253":"7348.01","1509937426286":"7348.01","1509937430066":"7345.54"}

Review same fiddle https://jsfiddle.net/Lmb5sd1m/1/

Upvotes: 3

Related Questions