title: How can I shuffle a JavaScript array?
body: <p>I am trying to randomize the order of my array in my Angular6 project. I have no idea how to do it and ended up trying to sort the array with the <code>Math.random()</code> function... (didn't work XD)</p>
  
  <p>This is my code so far:</p>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>    &lt;div style="background: darkcyan; width: 600px; height: 600px; margin: auto"&gt;
    &lt;table&gt;
      &lt;tr *ngFor="let card of cards"&gt;
        &lt;div id="{{card.id}}" [ngStyle]="{'background-color': card.color}" style=" width: 100px; height: 125px; margin: 5px"&gt;&lt;/div&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
  &lt;/div&gt;
  &lt;button (click)="shuffle()"&gt;Shuffle Cards&lt;/button&gt;
  </code></pre>
  
  <p><strong>TypeScript</strong></p>
  
  <pre><code>import { Component, OnInit } from '@angular/core';
  
  @Component({
    selector: 'app-memory-game',
    templateUrl: './memory-game.component.html',
    styleUrls: ['./memory-game.component.css']
  })
  export class MemoryGameComponent implements OnInit {
    cards = [];
    constructor() { }
  
    ngOnInit() {
      this.cards = [
          {
            'id': 1,
            'color': 'red'
          },
          {
              'id': 2,
              'color': 'green'
          },
          {
              'id': 3,
              'color': 'blue'
          },
          {
              'id': 4,
              'color': 'yellow'
          }
      ];
        this.shuffle();
    }
  
    public shuffle() {
        this.cards.sort(Math.random);
    }
  }
  </code></pre>
  
  <p>I don't know if there is an easy solution, but I really hope someone is able to help me out..</p>
  
  <p>Thanks</p>