You could create an array in your component (or provider) and push the cards id to it when it gets displayed. Then only display a card if it’s id is not in the array.
A better solution I think would be to add a
displayed: boolean;
property to your interface.
Initialize it to false on init.
Set it to true once it’s displayed.
Also add another array to your component.
Thank you for your answer. I also thought a boolean property could make it.
I am trying to implement your solution. But nothing happens when I click the button. I guess I am missing a point somewhere. Also, how can I change the value once it’s displayed ?
Hey there. I wrote my reply in a hurry and was using shorthand, so don’t quote it verbatim!
just apply it to what you aleady had.
for example, where I wrote
this.availableCards[rd]
It should be
this.randomCard = this.availableCards[rd];
As you wrote it initially.
I would, in ngOnInit()
set all cards to displayd: false.
export class RandomCardsPage implements OnInit {
cards: Card[];
availableCards: Card[];
randomCard: Card[]
// or should this be a single card? I can't tell. If so, randomCard: Card;
constructor() {
this.cards = [];
this.randomCard = [] //if it's supposed to be an array
this.availableCards = [];
}
ngOnInit() {
this.cards = cards.cards;
this.cards.map(card => card.displayed = false);
}
getRandom() {
this.availableCards = this.cards.filter(card => !card.displayed);
let rd = Math.floor(Math.random() * this.availableCards.length);
this.randomCard = this.availableCards[rd];
this.cards.find(card => card.id === this.randomCard.id).displayed = true;
}
}
Thx for the explanation. I am not sure, but I think randomCard shouldn’t be an array. If I understand your code, randomCard corresponds to the randomly chosen card among the available cards ? so it should be a one card.
I have this error : "Runtime Error. Error trying to diff ’ [object Object ]’. Only arrays and iterables are allowed."
Any idea ?