Convert Object into an Array

Aug 2 2020 9:53 AM
In development mode, items is an array but after I deploy the app, it turns into an object and I get the following error in the console:
Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
In the component:
  1. items = [];  
  2.   
  3. getStorageItems(): any[] {  
  4. try {  
  5. return JSON.parse(localStorage.getItem('items'));  
  6. catch(err) {  
  7. console.warn(err);  
  8. return [];  
  9. }  
  10. }  
  11.   
  12. ngOnInit(): void {  
  13. this.items = this.getStorageItems();  
  14. }  
In the HTML:
  1. <tr *ngFor="let item of items">  
  2. <td>{{item.quantity}} </td>  
  3. <td>{{item.product_name }}</td>  
  4. </tr>  

Brought to you by:

Answers (2)