I am trying to access data from a list, but I am having trouble using square brackets []. The getTalonPaie function calls the get method from the HttpClient service and returns an observable with multiple values. However, when I try to store these values in my array, it ends up being a strange empty list with data inside.
onSubmit(): void {
this.listeTalonPaie = Array<number>(1);
const test = [1, 2, 3];
this.listeIndividus = this.indS.listeIndividu;
this.listeIndividus.forEach(ind => {
// The following function returns an observable containing multiple objects
// which I want to add to my array. I am using the push method for a dynamic array
// as the number of objects returned by the observable is not static.
this.rcs.getTalonPaie(ind.id)
.subscribe( data => {
this.listeTalonPaie.push(data.heures);
test2.push(data.heures);
});
});
// The output shows an empty list:
// 1: 25
// 2: 40
// 3: 36
// length: 4
// __proto__ : Array(0)
console.log('listeTalonPaie ', this.listeTalonPaie);
// The output shows [null]
console.log('listeTalonPaie ', JSON.stringify(this.listeTalonPaie));
// The output is undefined
console.log('Un element ', this.listeTalonPaie[0]);
// The output is (3) [1, 2, 3]
console.log('test ', test);
}
If you have a better approach to suggest, please let me know as I'm unsure if this is the correct way to achieve my goal.