javascript - 在 Vue.js 中转换数组数组

标签 javascript arrays vue.js

如何在 Vue.js 中将数组的数组转换为单个数组?在我的 php 后端,我有下面的代码从数据库中获取数据。我现在的问题是我不知道如何在我的 js 端将它们转换为单个数组。

PHP 端:

$name = DB::raw("CONCAT(emp.first_name, ' ', emp.last_name) AS name");
$nameCol = [];
$usedDate = '2018-07';
$date = 'attendance.date';
$total  = 'attendance.total';

for($i = 1; $i<32; $i++){
    if($i<10) $i = '0'.$i;
        $names = DB::table('attendance')
                 ->leftJoin('m_emp','m_emp.emp_key','=','attendance.emp_key')
                 ->select($name,$date,$total)
                 ->where(DB::raw("(DATE_FORMAT(attendance.date,'%Y-%m-%d'))"), '=', $usedDate.'-'.$i)
                 ->get();
        array_push($nameCol,$names);
}
return (array)$nameCol;

结果是这样的:

enter image description here

每个数组内部是这样的:

enter image description here

最后里面是这样的:

enter image description here

我可以对它做 for 循环以将其转换为单个数组吗?如何做?或者我可以立即搜索其中的项目吗?因为我尝试过使用这样的方式进行搜索,但我认为这只能搜索或在单个数组中工作(这就是我想将它们合并到一个数组中的原因):

Vue.js 端

list.find( empName=> empName.name === 'John Doe')
//let's assume list is the variable that receives data returned from php
//result for this one is undefined.

知道怎么做吗?

最佳答案

您可以使用 array_merge 而不是 array_push

我猜你正在使用 Laravel ,你必须在合并之前将 collection 转换为 array ,参见示例:

$name = DB::raw("CONCAT(emp.first_name, ' ', emp.last_name) AS name");
$nameCol = [];
$usedDate = '2018-07';
$date = 'attendance.date';
$total  = 'attendance.total';

for($i = 1; $i<32; $i++){
    if($i<10) $i = '0'.$i;
        $names = DB::table('attendance')
                 ->leftJoin('m_emp','m_emp.emp_key','=','attendance.emp_key')
                 ->select($name,$date,$total)
                 ->where(DB::raw("(DATE_FORMAT(attendance.date,'%Y-%m-%d'))"), '=', $usedDate.'-'.$i)
                 ->get();
        // use array_merge
        $nameCol = array_merge($nameCol,$names->toArray());
}
return (array)$nameCol;

关于javascript - 在 Vue.js 中转换数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51435952/

相关文章:

javascript - 如何在 jQuery 中无限水平滚动图像?

javascript - Vue.js 调用组件中服务的方法

ruby-on-rails - 如何使用 webpacker gem 访问资源

javascript - 使用 v-for 填充 vue 中的多个选择菜单和选项

javascript - 如何传递对象数组以在 Vue 2.0 中选择选项

javascript - 如何仅针对同一个父级通过按钮隐藏/显示字段

javascript - 在模式中使用剑道编辑器时插入只读超链接弹出窗口

ios - 你可以在 Objective C 中将函数存储在数组中吗?

python - Python中多对的相同操作

c - OpenMP,并行写入不同的数组元素