0

I have an angularJS/Typescript application where I am trying to check if an object is already in a current list of objects

     if (this.selectedFormatData.indexOf(item) === -1) {
            //doesn't exist so add
            this.selectedFormatData.push(item);
        } else {
            this.selectedFormatData.splice(this.selectedFormatData.indexOf(item), 1);
        }

I have used this code before and it worked but isn't in this instance. Console output suggests it should work?

Any ideas?

Console display for objects

Update: yeah correct looks like a duplicate sorry. I had a previous bit of code where i thought it worked because it was returning 0 instead of -1. Not sure why it would return 0 though

enter image description here

72GM
  • 2,926
  • 3
  • 27
  • 33

1 Answers1

0

As the comments state, indexOf() is not meant to compare objects. This has been answered before here: Why Array.indexOf doesn't find identical looking objects.

Community
  • 1
  • 1