如何使用coffeescript迭代JSON哈希

时间:2022-09-11 15:26:02

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?

我是Coffeescript的新手,我遇到了解决问题的问题。我有一个当前存储在变量中的JSON对象。如何遍历JSON对象中的键以显示与之关联的键名和值?

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

Any help would be appreciated.

任何帮助,将不胜感激。

2 个解决方案

#1


47  

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

more in the docs#loops

更多的文档#循环

#2


1  

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

it's work!

是工作!

#1


47  

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

more in the docs#loops

更多的文档#循环

#2


1  

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

it's work!

是工作!