Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through JSON hash with coffeescript

I'm new to Coffeescript and I'm having issues resolving an issue. I have a JSON object that is currently stored in a variable. How do I iterate through the keys in the JSON object to display the key name and values associated with it?

if client
  result = JSON.parse client
  $.each result, (k, v) ->
    alert k + " is " + v

Any help would be appreciated.

like image 993
dspencer Avatar asked Oct 17 '12 19:10

dspencer


2 Answers

for key, value of result
  console.log "#{key} and #{value}"

more in the docs#loops

like image 146
zemirco Avatar answered Oct 16 '22 18:10

zemirco


result = JSON.parse client
     for c in result
        console.log(c.key +"-"+ c.value)

it's work!

like image 40
Nicolas Maturana Avatar answered Oct 16 '22 18:10

Nicolas Maturana