Problems with localStorage data caching.

the effect you want to achieve is like this: after entering the page, get the data to the page through the interface, save it, then exit the page, then re-enter the page, and then merge the data re-obtained from the page with the data saved last time. You can enter this page many times, and the data will be merged together, so you want to store it through localStorage.

enter the page, call the API, and request the data to be stored in list, and stored in localStorage:

localStorage.setItem("array", JSON.stringify(list));

then when you enter the page for the second time, the data of the second trip to Qingdao overwrites the data of the first time, which does not achieve the effect of merging. So it"s a bit of a mess, how do you need to modify it, or is there any other way to achieve it? I"m much obliged!

Mar.04,2022

get the localstorage data first, and then get the interface data
merge and then set in

let oldArr = localStorage.array | | [];
let newArr = list;

localStorage.array = [.oldArr, .newArr];


when you enter the page for the second time, take out the data saved for the first time, and then merge it together.


when entering the page, use a temporary variable to take out the value of localstorage and store it, then merge the values of the second request and then save localstorage.

Menu