javascript - 如何使用js从我的数据库中获取所有数组值
问题描述
1,这个js只取第一个数组值
2、我用foreach应该取所有的值
3,我很困惑请帮助
<script>
var unit_balance =
@foreach ($bike as $value)
[{{ $value->unit_balance }}];
@endforeach
function myFunction(){
var totalLessThan20 = unit_balance.filter(unit => unit <= 20);
if(totalLessThan20.length) {
alert("Some of unit left is less than 20. Please restock immediately!");
}
}
</script>
解决方案
var unit_balance = JSON.parse('{{ json_encode($bike->unit_balance ) }}');
在Javascript中引用传递(laravel)数组
推荐阅读
- makefile - 构建 AWS IoT 开发工具包时出现错误“源目录 ... 似乎不包含 CMakeLists.txt”
- java - 启用 ThriftParquetWriter 将 16 位整数写为短整数,而不是将其转换为 32 位整数
- python - 如何使全局变量在函数内部的 Spyder 中工作?
- laravel - 致命错误:已用完允许的 134217728 字节内存大小(尝试分配 53091665 字节)-Linode 服务器图像上传
- http - 如何在 POST 中的 JSON 中发送 JSON
- java - AIX - 无法创建 VM 线程。系统资源不足
- scala - 如何在 Play 框架的 Scala 模板中的 if 块中添加多个“OR”和“AND”条件
- llvm - 从分支指令中提取 MachineBasicBlock
- sql - Oracle Db:查询 Ctx_Ddl 首选项
- javascript - 如何将右键单击对话框上的画布保存为 jpg