H Min H Min - 11 months ago 51
JSON Question

get value of last occurrence of json element in javascript


in the above JSON response, I need to obtain the LAST objectId value to use as part of another URL. What is the best way to do this with javascript?

I attempted it with regex, but it got too messy. I'm assuming there is an easier way.


Try JSON.parse(...) on the source (since it is literally JSON) and then just treat it like an object literal.

Since it's an array you can do var array = JSON.parse(...) then get array[array.length - 1].objectID.