小编Mar*_*arf的帖子

我怎样才能改组JavaScript数组?

我试图在我的Angular6项目中随机化我的数组的顺序.我不知道怎么做,最后尝试用Math.random()函数对数组进行排序...(没有工作XD)

到目前为止这是我的代码:

HTML

    <div style="background: darkcyan; width: 600px; height: 600px; margin: auto">
  <table>
    <tr *ngFor="let card of cards">
      <div id="{{card.id}}" [ngStyle]="{'background-color': card.color}" style=" width: 100px; height: 125px; margin: 5px"></div>
    </tr>
  </table>
</div>
<button (click)="shuffle()">Shuffle Cards</button>
Run Code Online (Sandbox Code Playgroud)

打字稿

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': …
Run Code Online (Sandbox Code Playgroud)

javascript arrays random shuffle angular

2
推荐指数
1
解决办法
4687
查看次数

标签 统计

angular ×1

arrays ×1

javascript ×1

random ×1

shuffle ×1