I have a unique need for saving passwords in localStorage, so please don't try to figure out the reason behind it. I simply want to know if it can be done.
Here's the scenario: I have a registration form. If the localStorage is empty, I save an object in it. If there is already an object stored in localStorage, I retrieve it, push it into an array, and then add the new object to this array. Finally, I set this updated array back in localStorage.
const registrationForm = document.getElementById('registration');
registrationForm.addEventListener('submit', e => {
e.preventDefault();
let regName = document.querySelector(".registration__name").value;
let regPassword = document.querySelector(".registration__password").value;
let regEmail = document.querySelector(".registration__email").value;
let clientObj = {
name : regName,
password: regPassword,
email: regEmail
};
let clientsArr = [];
clientsArr = JSON.parse(localStorage.getItem('Users'));
if(!clientsArr) {
localStorage.setItem('Users', JSON.stringify(clientObj));
} else {
clientsArr.push(clientObj);
localStorage.setItem('Users', JSON.stringify(clientsArr));
}
})
Is this approach feasible? With this code, am I creating a new array each time and pushing objects endlessly?