我有一个类似于Ruby的Ruby数组:
[{:tour_no=>1, :occupied=>143, :signs_of_life=>133, :unoccupied=>166, :unusable=>136},
{:tour_no=>2, :occupied=>145, :signs_of_life=>171, :unoccupied=>166, :unusable=>136}]
我想在JavaScript数组中输出:tour_no
的值以使用图表库。
这是我通过将一些Ruby代码嵌入到我的HAML模板中尝试的内容:
categoryAxis: {
categories: [#{occupancy_by_tour.each do |tour| tour[:tour_no] end}],
majorGridLines: {
visible: false
}
},
我想要的(在JavaScript中):
categoryAxis: {
categories: [1, 2], // Set the tour counts for a survey here.
majorGridLines: {
visible: false
}
}
我目前得到的是什么:
categoryAxis: {
categories: [[{:tour_no=>1, :occupied=>143, :signs_of_life=>133, :unoccupied=>166, :unusable=>136}, {:tour_no=>2, :occupied=>145, :signs_of_life=>171, :unoccupied=>117, :unusable=>145}, {:tour_no=>3, :occupied=>136, :signs_of_life=>147, :unoccupied=>152, :unusable=>143}, {:tour_no=>4, :occupied=>141, :signs_of_life=>138, :unoccupied=>128, :unusable=>171}, {:tour_no=>5, :occupied=>150, :signs_of_life=>131, :unoccupied=>149, :unusable=>148}, {:tour_no=>6, :occupied=>137, :signs_of_life=>128, :unoccupied=>163, :unusable=>150}]],
majorGridLines: {
visible: false
}
}
为什么each
阻止输出JavaScript数组中的整个哈希内容?
答案 0 :(得分:1)
您需要使用map
函数将块中的每个值返回到模板:
categoryAxis: {
categories: #{occupancy_by_tour.map {|tour| tour[:tour_no]}},
majorGridLines: {
visible: false
}
},