0

So I have an object of goals. And I wanna see if one specific goal is inside.

I'm doing this:

for (let i = 0; i < this.goalsHome.length; i++) {
    console.log(this.goalsHome[i].includes(goal));
}

Which results in an error includes does not exists on type object.

But what If I want to check one specific object property ? Let's say I want to check if the goal's comment matches one of the goals comment in the object. That should be possible right ? By looping trough it?

image with the object

But if I add .comment in between it says comment doesn't exist on type object.

Serge K.
  • 5,303
  • 1
  • 20
  • 27
Sanne
  • 182
  • 2
  • 15

2 Answers2

2

You probably want to check if there is an object in the array with a comment matching the goals comment. If this is what you want to do you can do

this.goalsHome.filter(goalHome => goalHome.comment === goal.comment);

If you want to look for all matching objects. (See MDN Web Docs)

If you want to know whether a single match exists you can use the Array.prototype.some() method like so;

this.goalsHome.some(goalHome => goalHome.comment === goal.comment);

which will return true if there is atleast one match in the array (See MDN Web Docs).

Pieterjan
  • 445
  • 6
  • 23
  • Sadly that doesn't work either. I get this error: Property 'comment' does not exist on type 'object'. :( – Sanne Oct 06 '17 at 09:37
0

I ended up using stringify.

let goalFound = false;
let goalsSide;

    if (side == 'home') {
        goalsSide = this.goalsHome;
    } else {
        goalsSide = this.goalsAway;
    }

for (let i = 0; i < goalsSide.length; i++) {
            let stringGoals = JSON.stringify(goalsSide[i]);
            let stringGoal = JSON.stringify(goal);

            if (stringGoals == stringGoal) {
                goalFound = true;
                break;
            }
        }

        if (goalFound != true) {
            this.addGoalToArray(goal, side);
        }
Sanne
  • 182
  • 2
  • 15