Although I am not familiar with JSON and localStorage, I have a function that returns an object and a loop that repeats the function n times to create n objects.
function result(){
//some other functions
let finalObj = {
"Train Number" : getTrainNumber(),
"Departure Point" : cities[randomCity_A],
"Arrival Point" : cities[randomCity_B],
"Day of the Week" : getDepartureDay(),
"Departure Time" : mergedDepartureTime(),
"Cost" : getTicketPrice()
};
return finalObj;
}
var firingCount = prompt('Trains count', 10);
for(var i = 0; i < firingCount; i++) {
console.log(result());
let serialObj = JSON.stringify(result());
localStorage.setItem("myKey", serialObj);
}
I need assistance in saving all created objects in localStorage (it currently saves only 1 finalObj). Additionally, if possible, how can I save all objects in another .json file? Thank you.