I am developing a website where tickets for events can be bought. After selecting a ticket it is in the shopping cart. I have a sessionService.ts which basically is a Map<string, any>
that reads/writes from/to the local storage. Storing the map as a JSON string works perfectly but when I load the data from the storage and try to parse the JSON string to the Map the data is not consistent anymore.
console.log(JSON.parse(data))
shows the correct values but let x = JSON.parse(data); console.log(x);
shows manipulated data.
I tried recreating the problem on Stackblitz but I was unable to do that.
export class Ticket {
public id: number;
public created: Date;
constructor(
public reservation: boolean,
public price: number,
public section: number,
public row: number,
public seat: number,
public event: number | Event) {
}
}
private testParsing(){
const custom = '[["Tickets",[{"reservation":true,"price":10,"section":null,"row":10,"seat":10,"event":5,"id":1,"created":"2019-06-14T14:16:17.144Z"}]]]';
console.log('String to parse: ', custom);
console.log('String parsed directly to console: ', JSON.parse(custom));
}
console.log('String to parse: ', custom);
will display the correct values.
console.log('String parsed directly to console: ', JSON.parse(custom));
here the output is worng id = 0, row = null, seat = null.
Logs from code:
What causes this problem and how can I solve it?