How do I group values in array by month?

javascript group by month and year
javascript array group by sum
javascript group by multiple properties
json group by key javascript
angular 7 group by array
typescript groupby
moment js group by month
es6 group by

I'm trying to add sum all values of a specific key to an array devided into months, but can't figure out how to do it. I manage to get the first value, but trying to add on to that only gives me errors.

$accumulatedMonthly = DB::table('sold_tickets')
    ->select('price', 'created_at')
    ->where('event_id', $id)
    ->where('credited', null)
    ->where('user_id', '!=', null)
    ->orderBy('created_at')
    ->get()
    ->groupBy(function($val) {
        return Carbon::parse($val->created_at)->format('M y');
    });

$accumulatedMonthly = json_decode(json_encode($accumulatedMonthly), true);
$accumulatedPerMonth = [];

foreach ($accumulatedMonthly as $k => $month) {
    foreach ($month as $m) {
        $accumulatedPerMonth[$k] = $m['price'];
    }
}

I would like the result to be devided into months with all the 'price'-values added on top of eachother. Now I get the months correctly, but only the first value of each month.

This is the output currently

Array
(
    [Aug 16] => 999
    [Nov 16] => 1399
    [Dec 16] => 1399
    [Jan 17] => 1399
    [Feb 17] => 1599
    [Mar 17] => 1599
    [Apr 17] => 1599
    [May 17] => 1599
    [Jun 17] => 1599
    [Jul 17] => 1599
    [Aug 17] => 1199
)

Change

foreach ($accumulatedMonthly as $k => $month) {
    foreach ($month as $m) {
        $accumulatedPerMonth[$k] = $m['price'];
    }
}

to:

foreach ($accumulatedMonthly as $k => $month) {
    $accumulatedPerMonth[$k] = 0;
    foreach ($month as $m) {
        $accumulatedPerMonth[$k] += $m['price'];
    }
}

to get a sum of all the prices.

How to group objects with timestamps properties by day, week , Keep in mind that day, week and month are relative to origin, since epoch: function groupmonth(value, index, array) { d = new Date(value['date']); d = (d. Instead of assorted dates in a month, we have the common end of the month dates. For example, any dates in January 2018 will be converted to 31/01/2018, February 2018 will be converted to 28/02/2018 and so on. So we can group based on this end of the month dates which can act as a month wise grouping.


try collection pluck method, you will have array data.

update i have modified query.

$accumulatedMonthly = DB::table('sold_tickets')
    ->select(DB::raw('SUM("price") as price'), DB::raw("date_format('created_at','%M %y') as month"))
    ->where('event_id', $id)
    ->where('credited', null)
    ->where('user_id', '!=', null)
    ->orderBy('created_at')
    ->get()
    ->groupBy(DB::raw("date_format('created_at','%M %y')"))->pluck('price','month');

Easily Group Objects by a Date Property in Swift, sectioned by month. This is how I'm grouping my objects by their date property. We use reduce(into:) to transform our episodes array into a… # Group the data by month, and take the sum for each group (i.e. each month) df. resample ('M'). sum ()


You can do something like this:

    $accumulatedMonthly = DB::table('sold_tickets')
    ->select('price', 'created_at')
    ->where('event_id', $id)
    ->where('credited', null)
    ->where('user_id', '!=', null)
    ->orderBy('created_at')
    ->get();

     $accumulatedPerMonth = array();

     foreach ($accumulatedMonthly as $key => $value) 
     {
         if(array_key_exists(date('M y', strtotime($value->created_at)), $accumulatedPerMonth))
         {
             $accumulatedPerMonth[date('M y', strtotime($value->created_at))] += $value->price;  
         }
         else
         {
             $accumulatedPerMonth[date('M y', strtotime($value->created_at))] = $value->price; 
         }
     }

JavaScript group an array of objects by key, The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value. let group =  The right way to GROUP BY MONTH or DAY is to add YEAR to the GROUP BY clause. This way data will not be consolidated. For eg: now July 2007 and July 2008 are treated separate as a GROUP BY YEAR has been added. Here’s the same query with the GROUP BY YEAR clause


Grouping Date Objects by year and month, I'll focus first on static func sortDateByMonth(dateArray:[Date]) -> [[Date]]. because that contains the main logic, then mention some other points,  In Excel, an Array Formula allows you to do powerful calculations on one or more value sets. The result may fit in a single cell or it may be an array. An array is just a list or range of values, but an Array Formula is a special type of formula that must be entered by pressing Ctrl + Shift + Enter.


How to group an array of objects through a key using Array reduce , The single value can be number, string, object or another array. Lets see an usual example of array reduce,. // Person array with name and Age  given X as array of items you want to be grouped and y (1D array) as corresponding groups, following function does the grouping with numpy: def groupby(X, y): y = np.asarray(y) X = np.asarray(X) y_uniques = np.unique(y) return [X[y==yi] for yi in y_uniques]


group-array, Learn about our RFC process, Open RFC meetings & more.Join in the discussion​! » group-array. 1.0.0 • Public • Published a year ago. Basically this uses the reduce function to pass in an accumulator and current item, which then uses this to build your "grouped" arrays based on the passed in key. the inner part of the reduce may look complicated but essentially it is testing to see if the key of the passed in object exists and if it doesn't then create an empty array and