Laravel和Javascript中的对象到数组转换
我的项目有问题。我希望我的数据将以这种格式返回Laravel和Javascript中的对象到数组转换,javascript,jquery,laravel,google-maps,Javascript,Jquery,Laravel,Google Maps,我的项目有问题。我希望我的数据将以这种格式返回 var locations = [ ['Bondi Beach', 23.779328, 90.333686, 4], ['Coogee Beach', 23.815266, 90.359724, 5], ['Cronulla Beach', 23.824754, 90.414227, 3], ['Manly Beach', 23.804163, 90.456153, 2], ['Maro
var locations = [
['Bondi Beach', 23.779328, 90.333686, 4],
['Coogee Beach', 23.815266, 90.359724, 5],
['Cronulla Beach', 23.824754, 90.414227, 3],
['Manly Beach', 23.804163, 90.456153, 2],
['Maroubra Beach', 23.764990, 90.450636, 1]
];
这是我的控制器
public function map() {
Auth::user()->hasAccess([1, 8, 9]);
$sites = Site::orderBy('created_at', 'DESC')->select('siteId', 'latitude', 'longitude')->get();
return view('sites.map', compact('sites'));
}
我的Javascript代码是
var locations = {!! json_encode($sites) !!};
但它又回来了
var locations = [
{"siteId":"GPBNP1","latitude":"24.01144","longitude":"90.18492"},
{"siteId":"BHHZH1","latitude":"23.60953","longitude":"90.90608"}
];
以这种格式。我没有找到任何解决办法。任何反馈都会非常可观您可以这样更改格式:
var位置=[
{“siteId”:“GPBNP1”,“纬度”:“24.01144”,“经度”:“90.18492”},
{“siteId”:“BHHZH1”,“纬度”:“23.60953”,“经度”:“90.90608”}
];
var结果=locations.map((location,i)=>[location.siteId,location.latitude,location.longitude,i]);
console.log(result)
如果您想以更广泛的方式使用它,您可以使用toArray()
位置数组中的第四个数字代表什么?另外,您在查询中只选择了siteID、纬度和经度,而没有选择siteName。谢谢。你救了我一天
public function map() {
Auth::user()->hasAccess([1, 8, 9]);
$sites = Site::orderBy('created_at', 'DESC')->select('siteId', 'latitude', 'longitude')->get()->toArray();
return view('sites.map', compact('sites'));
}