Ante
Ante

Reputation: 125

Is there a way to compare an instance of class with elements in array of that class instances?

I have an array of objects that are created as new class Card instances and another instance of Card class that I want to find the index of in the array and splice:

...
this.cards.push(new Card(suit, rank));
>>>
        [
          Card { suit: 'Diamonds', rank: 'Jack' },
          Card { suit: 'Spades', rank: '3' },
          Card { suit: 'Spades', rank: 'Jack' },
          Card { suit: 'Diamonds', rank: 'Ace' },
          Card { suit: 'Clubs', rank: 'King' },
          Card { suit: 'Diamonds', rank: '6' },
          Card { suit: 'Spades', rank: '2' },
          Card { suit: 'Clubs', rank: '10' },
          Card { suit: 'Spades', rank: '7' },
          Card { suit: 'Clubs', rank: 'Queen' },
          Card { suit: 'Spades', rank: '10' }
        ]
    
const match = new Card('Clubs', rank: '10');
>>>     Card { suit: 'Clubs', rank: '10' }

A function should return the index of 7 when searching for the match in the given array. indexOf() method doesn't yield the desired outcome. I suspect that I am comparing locations in memory not the values, but don't know how to approach this problem.

Upvotes: 0

Views: 178

Answers (3)

connexo
connexo

Reputation: 56773

Your assumption

I suspect that I am comparing locations in memory not the values

is correct. Array.prototype.findIndex is your friend here:

function compareCards(a,b) {
  return Object.keys(a).every(key => a[key] === b[key]) && Object.keys(b).every(key => b[key] === a[key]);
}

const arr = [{
    suit: 'Diamonds',
    rank: 'Jack'
  },
  {
    suit: 'Spades',
    rank: '3'
  },
  {
    suit: 'Spades',
    rank: 'Jack'
  },
  {
    suit: 'Diamonds',
    rank: 'Ace'
  },
  {
    suit: 'Clubs',
    rank: 'King'
  },
  {
    suit: 'Diamonds',
    rank: '6'
  },
  {
    suit: 'Spades',
    rank: '2'
  },
  {
    suit: 'Clubs',
    rank: '10'
  },
  {
    suit: 'Spades',
    rank: '7'
  },
  {
    suit: 'Clubs',
    rank: 'Queen'
  },
  {
    suit: 'Spades',
    rank: '10'
  }
];

const cardToFind = {
  suit: 'Clubs',
  rank: 'King'
};

let index = arr.findIndex(card => compareCards(card, cardToFind));

console.log(index);

Upvotes: 1

phi-rakib
phi-rakib

Reputation: 3302

You can use findIndex method. It returns the index of the first element in an array that pass a test (provided as a function).

class Card {
  constructor(suit, rank) {
    this.suit = suit;
    this.rank = rank;
  }
}

let cards = [
  { suit: "Diamonds", rank: "Jack" },
  { suit: "Spades", rank: "3" },
  { suit: "Spades", rank: "Jack" },
  { suit: "Diamonds", rank: "Ace" },
  { suit: "Clubs", rank: "King" },
  { suit: "Diamonds", rank: "6" },
  { suit: "Spades", rank: "2" },
  { suit: "Clubs", rank: "10" },
  { suit: "Spades", rank: "7" },
  { suit: "Clubs", rank: "Queen" },
  { suit: "Spades", rank: "10" },
];

const match = (card, cards) => {
  return cards.findIndex((x) => x.suit === card.suit && x.rank === card.rank);
};

console.log(match(new Card("Clubs", "10"), cards));

Upvotes: 1

pynode
pynode

Reputation: 434

You can simply write a custom indexOf function to check the object.

function myIndexOf(o) {    
    for (var i = 0; i < this.cards.length; i++) {
        if (this.cards[i].suit == o.suit && this.cards[i].rank == o.rank) {
            return i;
        }
    }
    return -1;
}

Upvotes: 1

Related Questions