Ruby 数组到 Javascript 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8375059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby array to Javascript array
提问by Brian
I have a Ruby array with account ids.
我有一个带有帐户 ID 的 Ruby 数组。
I would like to store the Ruby array of account ids in a Javascript array.
我想将帐户 ID 的 Ruby 数组存储在 Javascript 数组中。
I was wondering the best way to do this?
我想知道这样做的最佳方法是什么?
In addition, when I've been trying to do this it seems that Javascript think if there is only one account id entered then that should be the size of the array. Is there a way around this? I've tried putting it in quotes but that does not seem to work.
此外,当我一直在尝试这样做时,Javascript 似乎认为如果只输入了一个帐户 ID,那么这应该是数组的大小。有没有解决的办法?我试过把它放在引号中,但这似乎不起作用。
回答by tokland
Let's assume you are using erb. A first approach:
假设您正在使用erb。第一种方法:
<%= javascript_tag "account_ids = #{account_ids.to_json.html_safe};" %>
The problem is that this creates a global variable without context (who uses it?). That's why I'd rather call a function defined somewhere in your JS code:
问题是这会创建一个没有上下文的全局变量(谁使用它?)。这就是为什么我宁愿调用在你的 JS 代码中定义的函数:
<%= javascript_tag "setAccounts(#{account_ids.to_json.html_safe});" %>
回答by ken
Here is the way works for me. Say I have array in controller:
这是对我有用的方法。假设我在控制器中有数组:
@my_array = [['city', 'number'], ['nyc', 39], ['queens', 98]]
I want to use it in slim and generate a Google pie chart, Then I can get that JavaScript array in slim by:
我想在 slim 中使用它并生成一个 Google 饼图,然后我可以通过以下方式获取该 JavaScript 数组:
javascript:
var myJsArray = #{raw @my_array};
or I can get that JavaScript array in erb like:
或者我可以在 erb 中获取该 JavaScript 数组,例如:
var myJsArray = <%=raw @my_array%>;
Somehow, works for me.
不知何故,对我有用。
回答by Shadwell
If in your controller you have:
如果在您的控制器中有:
@my_array = [1, 2, 3]
You can set a javascript variable like this in your view:
您可以在视图中设置这样的 javascript 变量:
<script type="text/javascript">
var myJSArray = new Array(<%= @my_array.map(&:to_s).join(", ") %>);
</script>
or:
或者:
var myJSArray = [<%= @my_array.map(&:to_s).join(", ") %>];
Both cases change your ruby array of numerical id values into an array of strings and then join those string values together with commas to output valid values for javascript.
这两种情况都会将数字 id 值的 ruby 数组更改为字符串数组,然后将这些字符串值与逗号连接在一起以输出 javascript 的有效值。
If you want string representations in your javascript you'll need to add double quotes around the values in your ruby array:
如果您想在 javascript 中使用字符串表示,则需要在 ruby 数组中的值周围添加双引号:
var myJSArray = [<%= @my_array.map { |some_id| '"' + some_id.to_s + '"' }.join(", ") %>];
回答by VP.
based on the last solution proposed by @Shadwell, i prefer something like:
基于@Shadwell 提出的最后一个解决方案,我更喜欢以下内容:
var myJSArray = new Array("#{@my_array.map(&:inspect).join(", ")}");
my example doesn't use ERB, because it was used on a MapReduce function for mongodb.
我的示例不使用 ERB,因为它用于 mongodb 的 MapReduce 函数。