簡體   English   中英

更好地重構地圖功能

[英]Better refactoring of map function

我目前正在重構我的方法。 我已經消除了漫長的foreach和if狀態,但是現在我在最后的整理方面苦苦掙扎,以擁有一個不錯的map函數。

我的方法看起來像。

/**
 * @param \Illuminate\Support\Collection
 * @return array
 */
public static function convertTimesToChartData($times) {
    $clientTotal = '0';
    $arrayIndex = 0;
    $chartData = collect($times)->filter(function($item) {
        return !is_null($item->end_time);
    })->map(function($item) use(&$clientTotal, &$arrayIndex){
        $clients[$arrayIndex]['id'] = $item->client_id;
        $clients[$arrayIndex]['project_id'] = $item->project_id;
        $clients[$arrayIndex]['label'] = $item->company;
        $clients[$arrayIndex]['sec'] = $item->end_time->timestamp - $item->start_time->timestamp;
        $clientTotal += $item->end_time->timestamp - $item->start_time->timestamp;
        $arrayIndex++;
        return $clients;
    })->flatMap(function($item) { return $item; });


    return $chartData;
}

在這里,我有2個問題:

  1. 有沒有更好的方法分配數組? 當我試圖直接在回報中分配時

      return [ [$arrayIndex]['id'] => $item->client_id, [$arrayIndex]['project_id'] => $item->project_id, [$arrayIndex]['label'] => $item->company, [$arrayIndex]['sec'] => $item->end_time->timestamp - $item->start_time->timestamp, ]; 

但是,然后我得到一個未定義的索引錯誤。

  1. 返回匯總數組的最佳方法是什么? 因為我為同一個客戶端有幾個時間條目,所以最后,我只想要摘要秒。 它適用於我的示例,但我認為有更好的方法可以做到這一點。 特別是因為我需要定義一個$ arrayIndex,我需要在map函數外部進行聲明並對其進行引用。 不要以為這是最好的方法。

這是原始來源:

    $projects = array();
    $projectTotal = '0';
    foreach($taskTimes as $time){
        if(!is_null($time->end_time))
        {
            if (isset($projects[$time->project_id]))
            {
                $projects[$time->project_id]['sec'] += $time->end_time->timestamp - $time->start_time->timestamp;
            } else
            {
                $projects[$time->project_id]['id'] = $time->client_id;
                $projects[$time->project_id]['label'] = $time->company;
                $projects[$time->project_id]['sec'] = $time->end_time->timestamp - $time->start_time->timestamp;
            }
            $projectTotal += $time->end_time->timestamp - $time->start_time->timestamp;
        }
    }

感謝您的任何建議和幫助!

為了獲得與原始的foreach循環相同的結果,您可以這樣做:

$projects = collect($taskTimes)
    ->filter(function ($time) {
        return !is_null($time->end_time);
    })
    ->groupBy('project_id')
    ->map(function ($group) {
        $group = collect($group);

        return [
            'id'         => $group->first()->client_id,
            'project_id' => $group->first()->project_id,
            'label'      => $group->first()->company,
            'sec'        => $group->sum(function ($item) {
                return $item->end_time->timestamp - $item->start_time->timestamp;
            }),
        ];
    });

$projectsTotal = $projects->sum('sec');

我已經將project_id添加到結果數組中,因為它包含在您的convertTimesToChartData()方法示例中

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM