Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
59
rated 0 times [  62] [ 3]  / answers: 1 / hits: 198308  / 14 Years ago, tue, november 30, 2010, 12:00:00

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')?


More From » object

 Answers
57

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.


[#94787] Saturday, November 27, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
parker

Total Points: 259
Total Questions: 109
Total Answers: 97

Location: Zambia
Member since Thu, Jun 25, 2020
4 Years ago
;