我有一个从下面的 map 函数返回的哈希数组。我正在尝试为图表填充数据(我正在使用AM 图表),该图表采用下面提到的特定格式的数据-
公司记录的样本结构
1.9.3p327 :073 > Company
=> Company(id: integer, name: string, contact_no: integer, email_id: string, website: string, fax_no: integer, created_at: datetime, updated_at: datetime, divisions_count: integer)
@all_companies = Company.all
地图功能:
@all_companies = @all_companies.map {|each_company| { Country: each_company.name, Visits: each_company.divisions_count} }
上述查询的输出格式
[{:country=>"Samsung", :visits=>8}, {:country=>"Natraj", :visits=>2}, {:country=>"Tupperware", :visits=>5}, {:country=>"Transcen", :visits=>0}, {:country=>"camlin", :visits=>0}]
图形数据输入格式:
var chartData = [{
country: "USA",
visits: 4025
}, {
country: "China",
visits: 1882
}, {
country: "Japan",
visits: 1809
}, {
country: "Germany",
visits: 1322
}]
我正在使用 Ruby 1.9.3-p327。我需要将我的输出数组转换为特定的图形格式。. 我尝试通过以下步骤进行相同的操作:-
1.9.3p327 :070 > @all_companies = @all_companies.to_s.gsub(":","")
=> "[{country=>\"Samsung\", visits=>8}, {country=>\"Natraj\", visits=>2}, {country=>\"Tupperware\", visits=>5}, {country=>\"Transcen\", visits=>0}, {country=>\"camlin\", visits=>0}]"
1.9.3p327 :071 > @all_companies = @all_companies.gsub("=>",": ")
=> "[{country: \"Samsung\", visits: 8}, {country: \"Natraj\", visits: 2}, {country: \"Tupperware\", visits: 5}, {country: \"Transcen\", visits: 0}, {country: \"camlin\", visits: 0}]"
1.9.3p327 :072 > puts @all_companies
[{country: "Samsung", visits: 8}, {country: "Natraj", visits: 2}, {country: "Tupperware", visits: 5}, {country: "Transcen", visits: 0}, {country: "camlin", visits: 0}]
=> nil
1.9.3p327 :073 >
现在,当我尝试在图表中显示数据时,出现以下语法错误:-
var chartData = [{country: "Samsung", visits: 8}, {country: "Natraj", visits: 2}, {country: "Tupperware", visits: 5}, {country: "Transcen", visits: 0}, {country: "camlin", visits: 0}]
来自 firebug 控制台的语法错误现在指向上述数据中的" 。
有什么解决方法可以帮助我完成最后一步,以便获得所需的图形格式?
问题更新
我需要以下格式的数据:-
var chartData = [
{
country: "Samsung",
visits: 8
},
{
country: "Natraj",
visits: 2
},
{
country: "Tupperware",
visits: 5
},
{
country: "Transcen",
visits: 0
},
{
country: "camlin",
visits: 0
}
]
谢谢你。