我的 CoffeeScript
errorList = @state.errors.responseText
for own key, value of errorList
console.log "#{key} -> #{value}"
我的 errorList 变量={"link":["is invalid"]}
当我运行此代码时,在输出中迭代此错误列表中的每个字符。我怎样才能得到“链接 -> 无效”?
最佳答案
您的responseText属性似乎是一个字符串,它还不是一个可迭代的对象。 要将这个(有效)JSON 字符串转换为我们可以迭代的对象,您应该调用 JSON.Parse。
以下内容应该适用于您的情况
errorList = JSON.parse @state.errors.responseText
for own key, value of errorList
console.log "#{key} -> #{value}"
输出:
link -> is invalid
关于javascript - 在 Coffeescript 中迭代 Json ResponseText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35061435/