我正在尝试使用 jQuery 插件 jQCloud 在 Rails 应用程序中创建词云。
词云数据需要的格式是:
var word_array = [
{text: "Lorem", weight: 15},
{text: "Ipsum", weight: 9},
];
我目前有一个词频的 ruby 散列,例如:
{"people"=>111, "other"=>110}
如何将其转换为所需的命名 javascript 数组,例如:
[{text: "people", weight: 11},{text: "other", weight: 11}]
如有任何想法或建议,我们将不胜感激!
谢谢
最佳答案
只需使用 map
:
hash.map { |k, v| { text: k, weight: v } }
=> [{:text=>"people", :weight=>111}, {:text=>"other", :weight=>110}]
关于javascript - 将 ruby 哈希转换为命名值的 javascript 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37593031/