3

Suppose I have an object with this structure:

{
        "friends_count": {
            "1420800660": 49391,
            "1421149814": 49344,
            "1421149955": 49344
        }
}

In the object, the first number (the key) is a timestamp. The second number is the value. I want to get the most recent item of that object. So, I need to get the key that is closest in time. How do I have to do it?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Jordi Romeu
  • 107
  • 2
  • 7
  • 3
    you have an object not an array – user428517 Jan 13 '15 at 16:06
  • 3
    http://book.mixu.net/node/ch5.html This is the first result I've found by writing "nodejs array" in google. It covers the basic of arrays, objects and JSON. The one you have is not an array, and the first result provided by google can explain this to you ;). Despite someone will answer, I would personally rather recommend you to first check some tutorials about javascript and, THEN, move into nodejs. – briosheje Jan 13 '15 at 16:07
  • Possible duplicate.. http://stackoverflow.com/questions/4317456/getting-the-last-item-in-a-javascript-object – khollenbeck Jan 13 '15 at 16:10
  • @KrisHollenbeck that dupe is for last in terms of position and disregards the key value. This is in terms of the key's orderable value itself, not purely position/insertion time. – ggorlen May 20 '21 at 23:11

2 Answers2

3

So, I need to get the key that is closest in time

Sure. Just call Object.keys on obj.friends_count and then sort

var key = Object.keys(obj.friends_count).sort()[0];

Object.keys returns an array of keys of the provided object and Array.sort will sort it in ascending order and [0] will take the first element of the sorted array.

Just Array.sort will work fine here since they are of the same length and everything should be fine. If you want to be more clear, then it would be arr.sort(function(a, b){ return a - b })[0]

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • 1
    I believe you need the last item of the array (unless the timestamps belong to the future), not the first one. Checking if the index exists would be a good idea, too. Up vote anyways:) – Michal Leszczyk Jan 13 '15 at 16:10
  • Default sort is wrong for numbers. It fits for this case but after many many years it won't. I think it's good practice to do it right. – Arūnas Smaliukas Jan 13 '15 at 16:35
  • @AmitJoki I don't think so. `["4", "30", "5"].sort()` will return `["30", "4", "5"]` and it's wrong – Arūnas Smaliukas Jan 13 '15 at 16:42
  • @ArūnasSmaliukas timestamps are of same length and in that case it will surely work. `["31", "40", "20"].sort()` will return `["20", "31", "40"]` – Amit Joki Jan 13 '15 at 16:47
1

If you are wanting to get the latest from the key timestamp then just convert it to a date:

var something = {
            "friends_count": {
                "1420800660": 49391,
                "1421149814": 49344,
                "1421149955": 49344
            }
    },
    dateCheck,
    newest;

for(var key in something.friends_count){
    if(!dateCheck || new Date(key*1000) > dateCheck){
        dateCheck = new Date(key*1000);
        newest = something.friends_count[key];
    }
}

console.log(newest);

http://jsfiddle.net/w4dsk4m3/

Simon Staton
  • 4,345
  • 4
  • 27
  • 49