Search code examples
javascriptecmascript-2016

.includes() on part of an object


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.


Solution

  • 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);
            }