Skip to content
Advertisement

How to get index of object in array of objects and splice it?

I’m using angular 1.3, but his question maybe related only to javascript.

My candidates array:

JavaScript

My peoples array:

JavaScript

And i’ve some checkbox too:

JavaScript

So i’ve a function that toggles an item (from candidates) and I want to add or remove (if already exists)

JavaScript

For some reason, if (idx > -1) its never true and it keeps add items even if they already exists.

Advertisement

Answer

indexOf will not compare object by value instead, it compare object by reference. You can do the following .

(Sorry updated my answer to get the “idx” and not if it exists )

You can do this :

JavaScript
Advertisement