H Min H Min - 1 year ago 97
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download