Reputation: 1
I am trying to loop my array using foreach and forloop in angular 8 application. But it is not iterating any element. I can not understand the issue. btw my array contains 250 objects. Does anyone know the issue?
Here is my code:
if (attributeValue.lovAvailable) {
console.log('LOV....', attributeObject.attributeLOVList);
attributeObject.attributeLOVList.forEach(element => {
console.log('element',element);
});
}
This is the console output of my list:
This is the expand list:
Upvotes: 0
Views: 611
Reputation: 2989
You are definitely trying to iterate the list before it is initialized.
You can see that in the screenshot of the console.log you provided. It shows []
* which means that the list is empty at that moment.
You need to make sure that you wait for the list to be initialized before you use it. E.g. if the list is passed as an @Input
you can't use it in the constructor, but in ngOnInit
.
*Note:
The log of the object in the console is not a snapshot, but a reference. That is why in the second image you had all the elements in the list after expanding it. By then it had been initialized (the top line still shows []
).
Edit:
To give an example about the console.log:
interface MyObject {
name: string;
}
let list: MyObject[] = [];
console.log(list);
console.log(list.length);
list.push({name: 'Hello'});
list.push({name: 'From'});
list.push({name: 'The'});
list.push({name: 'Other'});
list.push({name: 'Side'});
Upvotes: 3