5

I have a ruby hash that i want to convert into a specific javascript hash.
Here is the ruby hash keyval

{    
   "Former Administration / Neutral"=>24,     
   "Media Personality / P"=>2,     
   "Journalist / Neutral"=>32,   
   "Consultant / Neutral"=>2,

   ...     

   "Journalist / P"=>11, 
   "Expert / Neutral"=>1, 
   "Activist / Neutral"=>15 
}    

Into javascript hash

{data: "Former Administration / Neutral", frequency: (24) },
{data: "Media Personality / P", frequency: (2) },
{data: "Journalist / Neutral", frequency: (32) },
{data: "Consultant / Neutral", frequency: (2) },

 ...

{data: "Journalist / P", frequency: (11) },
{data: "Expert / Neutral", frequency: (1) },
{data: "Activist / Neutral", frequency: (15) }   

Tried

var obj = {};
for (var i = 0; i < <%= keyval.size %>; i++) {
obj["data"] = <%= keyval.keys[i] %>;
obj["frequency"] = '(' + <%= @keyval.values[i] %> + ')';
}

But the loop is not working obj return the first element of the ruby hash frequency=24 and does not escape the space in Former Administration. Why?

1
  • This definitly can not work, as i is a JS variable, but you are trying to use it in the RoR code. And you are overwriting the same object in every cycle of the loop. You need a Ruby loop, not a JavaScript loop. Commented Feb 6, 2014 at 14:50

3 Answers 3

4

There's a to_json method for converting ruby hashes and arrays into json objects. You could make an array of hashes using the first hash, then call to_json on it. Then, you're doing all your data manipulation in ruby, and just converting the format to json at the end.

hash = {    
   "Former Administration / Neutral"=>24,     
   "Media Personality / P"=>2,     
   "Journalist / Neutral"=>32,   
   "Consultant / Neutral"=>2,
   "Journalist / P"=>11, 
   "Expert / Neutral"=>1, 
   "Activist / Neutral"=>15 
}   
arr = []
hash1.each do |k,v|
  arr << {:data => k, :frequency => v}
end
arr.to_json

gives

"[{"data":"Journalist / P","frequency":11},{"data":"Activist / Neutral","frequency":15},{"data":"Former Administration / Neutral","frequency":24},{"data":"Expert / Neutral","frequency":1},{"data":"Journalist / Neutral","frequency":32},{"data":"Consultant / Neutral","frequency":2},{"data":"Media Personality / P","frequency":2}]"

You said that you wanted a "javascript hash", but what it looks like you have in your question, at the end, is an array without the square brackets. My result is a valid json object representation which is an array of objects. Which i think is actually what you want.

Sign up to request clarification or add additional context in comments.

2 Comments

How to get rid of the " and have "[{data:"Journalist / P",frequency:11},{data:"Activist / Neutral",frequency:15},{data:"Former Administration / Neutral","frequency":24},{data:"Expert / Neutral",frequency:1},{data:"Journalist / Neutral",frequency:32},{data:"Consultant / Neutral",frequency:2},{data:"Media Personality / P",frequency:2}]"
using arr.to_json.html_safe did the job.
2

You can dothis using map and join

keyval.map{|k,v| "{data: \"#{k}\", frequency: (#{v}) }" }.join(",\r\n")

the map method provides the key as k and the value as v in this example.

2 Comments

have an error doesn't like the " .Error: SyntaxError: syntax error {&quot;Former Administration / Neutral&quot;=&gt;44, &quot;Me
@mamesaye you need to add .html_safe at the end of the statement if you render it inside an HTML page.
1

Try this:

var data = [];
<% for i in [email protected] { %>
  data.push({});
  data[<%= i %>]["data"] = <%= @keyval.keys[i] %>;
  data[<%= i %>]["frequency"] = '(' + <%= @keyval.values[i] %> + ')';
<% } %>

So you will get an array of those objects.

1 Comment

that was helpful, thanks. just change some stuff for it to work var data = []; <% for i in [email protected] %> data.push({}); data[<%= i %>]["data"] = "<%= @keyval.keys[i] %>"; data[<%= i %>]["frequency"] = '(' + <%= @keyval.values[i] %> + ')'; <% end %> console.log(data); but is is adding object before each element... [Object { data="Former Administration / Neutral", frequency="(24)"}, Object { data="Media Personality / P", frequency="(2)"}, ...] How to get rid of it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.