Check if an object exist in an array in Angular 6

Unfortunately, includes will not work unless it is the same instance. You will need to do something like;

if (!this.categories.some((item) => item.id == newCategory.id)) {
    this.categories.push(newCategory);
}

The solution given by user184994 didn't worked for me, It was only fetching the first element. I tried like this which worked for me.

let data = this.categories.find(ob => ob.id === newCategory.id);
if(data === null){
 this.categories.push(newCategory);
}

Tags:

Angular