[英]Remove " when printing an array in the JavaScript console
I have an array that I am passing to a javascript file. 我有一个数组要传递给javascript文件。
In the controller I have the array printing out and it appears like so... 在控制器中,我已经打印出阵列,它看起来像这样...
["123456789", "987654321", "147852369", "369852147"]
This array is set to the instance variable @array in the controller. 该数组在控制器中设置为实例变量@array。
I'm sending this array to a JavaScript file by setting @array
as a variable in the view. 我通过将
@array
设置为视图中的变量来将此数组发送到JavaScript文件。
var array = "<%= @array %>";
Note: I've tried var array = "<%=raw @array.to_json %>";
注意:我已经尝试过
var array = "<%=raw @array.to_json %>";
as well. 也一样
Then in the JavaScript file I am simply printing the array in the console like so... 然后在JavaScript文件中,我只是像这样在控制台中打印数组。
console.log(array);
However when it is printed in the console the output looks like this. 但是,当它在控制台中打印时,输出看起来像这样。
["123456789", "987654321", "147852369", "369852147"]
When I check the typeof through jQuery it comes back as a string instead of an array. 当我通过jQuery检查typeof时,它以字符串而不是数组的形式返回。
I do not want to see the "
我不想看到
"
. 。 Eventually I want to use these values as selectors when running some JavaScript functions.
最终,我想在运行某些JavaScript函数时将这些值用作选择器。
Through these steps, what am I doing wrong that is producing the "
通过这些步骤,我做错了什么
"
? ?
edit: This is how I am initially creating my array. 编辑:这就是我最初创建数组的方式。
CONTROLLER
@original= large_array_of_hashes
# I am grabbing only specific data from the hash. Only what comes after @errors=>
@data = @original.scan(/@errors=>"(.*?)"/).flatten.map{ |msg| msg.gsub(/(\.|\s+)/, '').strip }
@array = []
@data.each{|data| @array << data}
My issue was corrected by just prepending raw to my ruby variable 我的问题已通过仅将raw前置到我的ruby变量中而得到解决
<%= raw @array %>
I know it's late but hope it helps someone down the road! 我知道已经晚了,但希望它对以后的工作有所帮助!
I think that 我觉得
@array = %w[123456789 987654321 147852369 369852147]
and then your javascript things will work. 然后您的javascript东西就可以了。
TRy this, place them in single quotes 尝试一下,将它们放在单引号中
IN javascript 在javascript中
var array = '<%= @array.to_json %>';
array.replace(/"/g, '"');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.