title: How can I push to a subarray by grouping messages it in JS?
body: <p>I'm not very sure how I should name this question but I'll try to explain my problem. At the moment I've an array in PHP with some entries.</p>
  
  <pre><code>Array
  (
      [0] =&gt; stdClass Object
          (
              [sender_id] =&gt; 0
              [message] =&gt; Test
              [created_at] =&gt; 2020-03-12 16:26:23
          )
  
      [1] =&gt; stdClass Object
          (
              [sender_id] =&gt; 0
              [message] =&gt; Hello
              [created_at] =&gt; 2020-03-12 13:47:47
          )
  )
  </code></pre>
  
  <p>What I needed was a way to group each entry by the created_at date so that my result looked like this: </p>
  
  <pre><code>Array
  (
      [2020-03-12] =&gt; Array
          (
              [0] =&gt; stdClass Object
                  (
                      [sender_id] =&gt; 0
                      [message] =&gt; Test
                      [created_at] =&gt; 2020-03-12 16:26:23
                  )
  
              [1] =&gt; stdClass Object
                  (
                      [sender_id] =&gt; 0
                      [message] =&gt; Hello
                      [created_at] =&gt; 2020-03-12 13:47:47
                  )
          )
  
  )
  </code></pre>
  
  <p>To reach this I've done this in PHP:</p>
  
  <pre><code>$grouped = [];
  
  foreach ( $messages as $message ) {
      $created_at_date = date( 'Y-m-d', strtotime( $message-&gt;created_at ) );
      $grouped[ $created_at_date ][] = $message;
  }
  </code></pre>
  
  <p>But now I'v got an issue so I decided to do this task in JS. So first I've encoded the array in PHP and set it to a variable inside a <code>&lt;script&gt;</code> tag:</p>
  
  <pre><code>&lt;script&gt;
      let messages = &lt;?= ! empty( $messages ) ? json_encode( $messages ) : '[]' ?&gt;;
  &lt;/script&gt;
  </code></pre>
  
  <p>Now I've got a lot of messages but they are not grouped yet. Because I need to respect the timezone, I can only group them in JS where I know the clients timezone. I've already done the thing with the timezone but now I can't get the messages grouped like in PHP. This is what I've tried: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let messages = [ {
   sender_id: "0",
   message: "Test",
   created_at: "Thu Mar 12 2020 17:26:23 GMT+0100 (Mitteleuropäische Normalzeit)"
  }, {
   sender_id: "0",
   message: "Hallo",
   created_at: "Thu Mar 12 2020 17:26:23 GMT+0100 (Mitteleuropäische Normalzeit)"
  } ];
  
  let groupedMessages = [];
  
  jQuery( messages ).each( function ( index, message ) {
   let createdAtDate = new Date( message["created_at"] ).toLocaleDateString( navigator.language, {
    day: "2-digit",
    month: "2-digit",
    year: "numeric"
   } );
    
   groupedMessages[createdAtDate][] = message;
  } );
  
  console.log( groupedMessages );</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>