0

我不确定如何使用咖啡脚本修改 JSON 文件格式 我有这样的东西

{"counts":{"USA":100,"France":90,"Italy":80,"Canada":70,"Germany":60}}

我想把json转换成这个

[{text: "USA", weight: 100},{text: "France", weight: 90},{text: "Italy",weight: 80},{text: "Canada", weight: 70}, {text: "Germany", weight: 60}]

这在 Java 中很容易,但不确定咖啡脚本

4

1 回答 1

1

我会使用的 CoffeeScript 功能:

  • 数组推导(for循环的返回值)
  • for key, value of object环形
  • 对象文字comisition shorthard{ foo }相同{ foo: foo }

例子:

data = {"counts":{"USA":100,"France":90,"Italy":80,"Canada":70,"Germany":60}}

result =
  for text, weight of data.counts
    { text, weight }

console.log result
# [{"text":"USA","weight":100},{"text":"France","weight":90},{"text":"Italy","weight":80},{"text":"Canada","weight":70},{"text":"Germany","weight":60}]
于 2013-06-27T18:45:16.497 回答