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;

 

 

 

Saved you some valuable time?

Received 100 so far from 4 kind contributors.