How can I get the last item in a JavaScript object?

javascript, object

Solution

No. Order is not guaranteed in JSON and most other key-value data structures, so therefore the last item could sometimes be `carrot` and at other times be `banana` and so on. If you need to rely on ordering, your best bet is to go with arrays. The power of key-value data structures lies in accessing values by their `keys`, not in being able to get the `nth` item of the object.

Problem

If I have an object like: ``` { 'a' : 'apple', 'b' : 'banana', 'c' : 'carrot' } ``` If I don't know in advance that the list goes up to 'c', other than looping through the object, is there a way to get the last item in the object (e.g. `'carrot'`)?

Original source

Related problems