Using JSON I am constructing a Javascript class that keep data of a players goals over a season.
Within the Constructor of the class the season is an array but the goals are contained in and additional object within the array.
class BuildGoalsDetail {
constructor(a){
this.season = [];
this.season = new Object([]); // The problem is that this isn't getting included in when using stringify
this.season.goals = [];
}
buildPlayerData(a0, a1){
this.season.push(a0);
this.season.goals.push(a1);
}
}
player = new Array();
player.push();
player[0] = new BuildGoalsDetail();
player[0].buildPlayerData(1971,7);
player[0].season.goals.push(12);
player[0].season.goals.push(8);
player[0].season.goals.push(9);
document.write("Season: "+player[0].season+" Goals: "+player[0].season.goals+"<br/><br/>");
player.push();
player[1] = new BuildGoalsDetail();
player[1].buildPlayerData(1974,7);
player[1].season.goals.push(10);
player[1].season.goals.push(12);
document.write("Season: "+player[1].season+" Goals: "+player[1].season.goals+"<br/><br/>");
a0 = JSON.stringify(player);
document.write("Output of JSON:<br/>"+a0);
The output works fine but I then want to save this as JSON, when I use Stringify it only serialises the year array and not the object with the goals array.
If Stringify isn’t the best method is there another way I should be looking at?