在 Javascript 中传递(laravel)数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33326699/
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
Passing (laravel) Array in Javascript
提问by senty
I want to pass/store Laravel array in JavaScript variable. I used ->all()
so I get the result like this rather than object:
我想在 JavaScript 变量中传递/存储 Laravel 数组。我使用了->all()
所以我得到了这样的结果而不是对象:
array:83 [▼
0 => 1
1 => 11
2 => 12
...
]
I can access this in view using {{ $theArray }}
.
我可以使用{{ $theArray }}
.
However whatever I tried, I couldn't make this to javascript array.
但是,无论我尝试过什么,我都无法将其转换为 javascript 数组。
I tried
我试过
var array = {{ $theArray }};
var array = {{ $theArray }};
var array = {{{ $theArray }}};
var array = {{{ $theArray }}};
I feel like I'm close but I couldn't figure it out
我觉得我很接近但我无法弄清楚
回答by Alex Alekser
var app = @json($array);
Works like a charm
奇迹般有效
回答by Pranav C Balan
you can use json_encode()
您可以使用 json_encode()
var array = {{ json_encode($theArray) }};
or parse the json string using JSON.parse()
或使用解析json字符串 JSON.parse()
var array = JSON.parse('{{ json_encode($theArray) }}');
回答by Francisca GV
This works for me :)
这对我有用:)
var array = {!! json_encode($theArray) !!};
回答by Niranjan N Raju
you have enclodse with quotes,or use json_encode()
你用引号括起来,或者使用 json_encode()
var array = "{{ $theArray }}";
^ ^
or, if the value in an array()
或者,如果一个值 array()
var array = "{{ json_encode($theArray) }}";
^ ^
Without having quotes around javascript variable, it will throw you error. you can check in your console.
如果在 javascript 变量周围没有引号,它会抛出错误。你可以检查你的控制台。
回答by Ajay
this one worked for me.
这个对我有用。
let array = JSON.parse('{!! json_encode($array) !!}');
let array = JSON.parse('{!! json_encode($array) !!}');
回答by Zain
Sometimes you may pass an array to your view with the intention of rendering it as JSON in order to initialize a JavaScript variable. For example:
有时,您可能会将一个数组传递给您的视图,目的是将其呈现为 JSON 以初始化 JavaScript 变量。例如:
<script>
var app = <?php echo json_encode($array); ?>;
</script>
However, instead of manually calling json_encode
, you may use the @json
Blade directive. The @json
directive accepts the same arguments as PHP's json_encode
function:
但是,json_encode
您可以使用@json
Blade 指令代替手动调用。该 @json
指令接受与 PHPjson_encode
函数相同的参数:
<script>
var app = @json($array);
var app = @json($array, JSON_PRETTY_PRINT);
</script>
The @json
directive is also useful for seeding Vue components or data-* attributes:
该@json
指令对于播种 Vue 组件或 data-* 属性也很有用:
<example-component :some-prop='@json($array)'></example-component>
https://laravel.com/docs/5.8/blade#blade-and-javascript-frameworks
https://laravel.com/docs/5.8/blade#blade-and-javascript-frameworks
回答by Aman
Simply, escape the special characters using the blade syntax. Try this.
简单地,使用刀片语法转义特殊字符。尝试这个。
var array = {!! $theArray !!};
Best part, you don't need to parse it to its object form in JavaScript.
最好的部分是,您不需要在 JavaScript 中将其解析为对象形式。
回答by Valentine Shi
Sometimes all()
is not enough to convert your Laravel collectionto array. I encountered this issue trying to pass the collection of custom type objects to JS via Laravel view.
有时all()
将Laravel 集合转换为数组是不够的。我在尝试通过 Laravel 视图将自定义类型对象的集合传递给 JS 时遇到了这个问题。
To get the array on the front end JS you have to apply Collection::values()
method before all()
method.
要在前端 JS 上获取数组,您必须在Collection::values()
方法之前应用all()
方法。
So:
所以:
// In your HTTP controller
$collection = collect([myObj1,myObj2]); // The collection filled with custom type objects.
$myArray = $collection->values()->all(); // Then converted to an array. Just `all()` is not enough;
return view('myview', $myArray);
{{-- In your myview.blade.php --}}
<script>window.myArray= @json($myArray)</script>
Then in your JS window.myArray
you get an array []
, not an object {}
.
然后在你的 JS 中window.myArray
你得到一个数组[]
,而不是一个对象{}
。
A Bit Of Details
一些细节
This is probably happens due to the fact that when an array indexes are not ascending ordered PHP considers the indexes to be object keys, then it considers the array to be an object. Thus the transformation to an object instead of an array. Laravel collection values()
resets the array keys. I suspect it applies PHP array_values()
under the hood.
这可能是因为当数组索引不是升序时,PHP 将索引视为对象键,然后将数组视为对象。因此转换为对象而不是数组。Laravel 集合会values()
重置数组键。我怀疑它array_values()
在幕后应用了 PHP 。