Answer the question
In order to leave comments, you need to log in
Why is the array overwritten?
There is a cycle
arr = [];
......
......
this.httpService.getList().subscribe(data =>
{
this.arr = data;
for(let i=0; i<this.arr.length; i++) {
this.arr.push(this.arr[i]);
// this.arr = [];
}
}
);
Answer the question
In order to leave comments, you need to log in
And what did you want to do?
Your loop will never end since you are constantly adding elements to the array. Before each iteration there is a check:
And you have both i and this.arr.length after each iteration increase by 1 and as a result this.arr.length is always greater.
Perhaps you wanted to do something like this:
this.httpService.getList().subscribe(data => {
this.arr = [...this.arr, ...data];
});
this.httpService.getList().subscribe(data => {
this.arr = this.arr.concat(data);
});
this.httpService.getList().subscribe(data => {
data.forEach(el => this.arr.push(el));
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question