gpt4 book ai didi

JavaScript 不喜欢 hashes.to_json?

转载 作者:行者123 更新时间:2023-11-30 17:44:06 26 4
gpt4 key购买 nike

在我的 Controller 中:

@arr = []
@jsonPosts = Post.each do |p|
arrLatLng = {}
arrLatLng["latitude"] = p.lat
arrLatLng["longitude"] = p.lon
@arr << arrLatLng
end
@postsLatLng = @arr.to_json

当我使用 console.log(<%= @postsLatLng %>) 时,在 View 中我得到了一个意外的标记和错误,它显示:

console.log([{&quot;latitude&quot;:34.0462002,&quot;longitude&quot;:-118.2044673},{&quot;latitude&quot;:34.0462074,&quot;longitude&quot;:-118...

当我更改 arrLatLng 时它会起作用到数组:

arrLatLng = []
arrLatLng << p.lat
arrLatLng << p.lon

我在想:

  1. 解决方法是什么?
  2. 为什么当我更改 arrLatLng 时它会起作用到数组?

最佳答案

尝试 http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-j

console.log(<%= j @postsLatLng %>)

此外,完全不相关,但您可以改为这样做:

@postsLatLng = Post.inject([]) { |memo, p|
memo << {latitude: p.lat, longitude: p.lon}
}.to_json

关于JavaScript 不喜欢 hashes.to_json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20450096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com