let's say we have an array of objects as shown below:
[
0: {'id': 0, 'title': 'ABC', value: '113' },
1: {'id': 1, 'title: 'BCD', value: '242' },
...
99: {'id': 99, 'title: 'ZAQ', value: '971' },
]
The array of objects are information required in different component of the application. For example, component A
only needs to obtain the updated data of id=0
while component B
only needs id=2
, id=6
. The array of objects will change overtime, thus, there is a subject that contains it.
The array of changed objects is emitted to the various components show below:
private testSubject = new Subject<any>();
public notifyTestChanged(changedData: any): void {
this.testSubject.next(changedData);
}
public onTestChanged(): Observable<any> {
return this.testSubject.asObservable();
}
The different component subscribe to testSubject
to get the array and i am looping through the arrays to find if the object exists in the changed object array.
onTestChanged.subscribe((changedData) =>
changedData.find((data) =>
return componentchanged.id === data.id))
// componentchanged.id is a parameter in the component, an identity for component.
Is there a way to subscribe to the changes without needing to loop through the array of objects in each components to get the desired data the component needed? I am avoiding creating 1 subject for each object as the array of objects may contain hundreds of objects.