ruby on rails - Remove " when printing an array in the JavaScript console - Stack Overflow

I have an array that I am passing to a javascript file. In the controller I have the array printing out

I have an array that I am passing to a javascript file.

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.

I'm sending this array to a JavaScript file by setting @array as a variable in the view.

var array = "<%= @array %>";

Note: I've tried var array = "<%=raw @array.to_json %>"; as well.

Then in the JavaScript file I am simply printing the array in the console like so...

console.log(array);

However when it is printed in the console the output looks like this.

[&quot;123456789&quot;, &quot;987654321&quot;, &quot;147852369&quot;, &quot;369852147&quot;]

When I check the typeof through jQuery it es back as a string instead of an array.

I do not want to see the &quot;. Eventually I want to use these values as selectors when running some JavaScript functions.

Through these steps, what am I doing wrong that is producing the &quot;?

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 es after @errors=>
  @data = @original.scan(/@errors=>"(.*?)"/).flatten.map{ |msg| msg.gsub(/(\.|\s+)/, '').strip }
  @array = []
  @data.each{|data| @array << data}

I have an array that I am passing to a javascript file.

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.

I'm sending this array to a JavaScript file by setting @array as a variable in the view.

var array = "<%= @array %>";

Note: I've tried var array = "<%=raw @array.to_json %>"; as well.

Then in the JavaScript file I am simply printing the array in the console like so...

console.log(array);

However when it is printed in the console the output looks like this.

[&quot;123456789&quot;, &quot;987654321&quot;, &quot;147852369&quot;, &quot;369852147&quot;]

When I check the typeof through jQuery it es back as a string instead of an array.

I do not want to see the &quot;. Eventually I want to use these values as selectors when running some JavaScript functions.

Through these steps, what am I doing wrong that is producing the &quot;?

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 es after @errors=>
  @data = @original.scan(/@errors=>"(.*?)"/).flatten.map{ |msg| msg.gsub(/(\.|\s+)/, '').strip }
  @array = []
  @data.each{|data| @array << data}
Share Improve this question edited Mar 19, 2015 at 19:36 mikeymurph77 asked Mar 19, 2015 at 19:09 mikeymurph77mikeymurph77 8021 gold badge12 silver badges28 bronze badges 4
  • Try <%=raw @array.to_json.html_safe %> – Sonalkumar sute Commented Mar 19, 2015 at 19:20
  • Unfortunately that did not work... I got an SyntaxError: Unexpected number error – mikeymurph77 Commented Mar 19, 2015 at 19:23
  • TRy this <%= @array.to_json.flatten.to_json.html_safe %> or just <%= raw @array %> – Sonalkumar sute Commented Mar 19, 2015 at 19:24
  • The first option gave me an internal server error and the second method gave me the same SyntaxError: Unexpected number .... I've updated my question to include how I am creating my array. – mikeymurph77 Commented Mar 19, 2015 at 19:37
Add a ment  | 

3 Answers 3

Reset to default 4

My issue was corrected by just prepending raw to my ruby variable

<%= raw @array %>

I know it's late but hope it helps someone down the road!

TRy this, place them in single quotes

IN javascript

var array = '<%= @array.to_json %>';
array.replace(/&quot;/g, '"');

I think that

@array = %w[123456789 987654321 147852369 369852147]

and then your javascript things will work.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745313553a4622111.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信