How to find object with id from array of objects and manipulate it in TypeScript

The other day I had this typical thing to do in my function:

Find a favorite with a certain id, and attach notifications to it.

Solution

The solution (one of the possible solutions) is to find the index with findIndex method and then manipulate the object in the original array of objects with that index:

// this.favorites is an array of Favorite objects
// Notification has a favoriteId identifier
const foundIndex = this.favorites.findIndex((favorite: Favorite) => favorite.id === notification.favoriteId);
if (foundIndex > -1) this.favorites[foundIndex].notifications = notification;