I am facing an issue with a BehaviorSubject where the first .subscribe callback is returning an Array with 6 Objects. Strangely, in console output, it shows length: 6
, but every for-loop I iterate through the array only runs 5 times and even when I log arr.length, it outputs '5'.
I'm uncertain how to replicate this problem, but here's the code snippet:
public objects: BehaviorSubject<Object[]> = new BehaviorSubject([]);
// other class
this.objectService.objects.subscribe(_objects => {
if (_objects) {
for (const obj of _objects) {
console.log(obj);
}
console.log(_objects);
console.log(_objects.length);
}
});
Output:
How could this discrepancy occur? Various services are writing to this BehaviorSubject using objects.next, so how can I prevent this inconsistency from happening? This issue seems to only manifest when the 'objects' property is updated for the first time. Subsequent updates work fine and display the correct length.
I've also tried using .find and .filter on the array, but they seem to only filter the last 5 entries and not the first one.