On the problem of storing data in localStorage

enter the page, call the interface, request to the data list, and then store the list in localStorage:

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

then exit the page, enter again, and get the data again. What does list, need to do now to push this newly requested list to an existing localStorage? If you try for a long time, you will only cover it, but you can"t push it. Please help! Thank you very much!


although you do not approve of this, but if you insist on doing so, you can create a new array variable. Every time you call the interface, the array returned by the push interface will be put into the array variable, and then stored.

var arr = [];
arr.push(list);
localStorage.setItem("array", JSON.stringify(arr));

I don't understand why you want to operate in this way. You have re-acquired the data in list, reasonable localStorage is out-of-date data, so there is no problem with overwriting. If your original intention is to update the data in localStrorage, you can insert it after comparing the old data with the new data


.

arr / / New array

let newArr=JSON.parse(localStorage.array).push(arr)
localStorage.setItem("array", JSON.stringify(newArr));
Menu