简体   繁体   English

从哈希中取出值并插入到ruby中的数组中

[英]Take out values from hash and insert in an array in ruby

I am having 我有

students = {1=>"alexandar", 2=>"mona", 3=>"gaga", 4=>"", 5=>"tom", 35=>"abcd", 44=>"hin"}
array = []

I had given this 我给了这个

array = students.values

and when I write in my test.js.erb 当我写我的test.js.erb

$('.show-test').text('<%= array%>')

it gives o/p 它给出了o / p

[&quot;alexandar&quot;, &quot;mona&quot;, &quot;gaga&quot;,
&quot;tom&quot;, &quot;abcd&quot;, &quot;hin&quot;]

Now I need to show text like this 现在我需要显示这样的文字

 [alexander, mona, gaga, tom, abcd, hin]

Please guide me how to solve this. 请指导我如何解决此问题。 Thanks in advance 提前致谢

Super simple, use Hash#values : 超级简单,使用Hash#values

$> students = {1=>"alexandar", 2=>"mona", 3=>"gaga", 4=>"", 5=>..............}
$> students.values # <- Array with values
=> ["alexandar", "mona", "gaga", "", "tom", "abcd", "hin"]

Update: 更新:

Use html_safe if you trying to render json from controller. 如果尝试从控制器呈现json,请使用html_safe

$('.show-test').text('<%= array.html_safe %>') 

or: 要么:

$('.show-test').text('<%= array.to_json.html_safe %>')

or (the same thing as html_safe ): 或(与html_safe相同):

$('.show-test').text('<%= raw(array.to_json) %>')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM