My localStorage
is only saving a few variables (3) of similar names, and then proceeding to override the last variable with the new value.
What I am trying to do is add variables into localStorage
that have a name and a number attached to them such as Test1, Test2, Test3, etc.
The only issue is that after the third element, in this case Test3, the key gets overridden to Test4 and the value changes to the new value. This happens forever as long as the word Test is the same.
I can add other values just fine, but only up to 3 of the same root word.
This is the code I am using to add the elements:
const AddToLocalStorage = (type, contents) => {
let ind = 0;
Object.keys(localStorage).forEach(function (key) {
if (key == (type + ind)) {
ind++;
} else {
return;
}
});
localStorage.setItem((type + ind), JSON.stringify(contents));
}
type
is a string such as Test
contents
is the value stored
Thanks in advance :)
Edit - Can you clarify how to call the AddToLocalStorage
function
AddToLocalStorage("Test", "value");
In localStorage
this would set like { "Test0", "value" }