I have a class:
export class CartItem {
productname!: string;
quantity!: number;
}
I store an array of this in session as:
cartItems: Array<CartItem> = [];
...
...
sessionStorage.setItem("CurrentCart", JSON.stringify(this.cartItems));
When retrieving this, I tried:
this.cartItems = JSON.parse(sessionStorage.getItem("CurrentCart") || '') as Array<CartItem>;
But this returns:
Cart Items [object Object],[object Object],[object Object],[object Object]
and not the array with values.
From what I see in some examples Angular convert json to array of object, seems I need to map the array back or write a custom transformer https://medium.com/typescript-center/casting-json-objects-to-typescript-classes-a-deep-dive-835b8f8988bf
Is that the only way? Seems inefficient and cumbersome for bigger classes.