I stored a group of users in the local storage:
let btnSignUp = document.getElementById("btnSignUp");
btnSignUp.addEventListener('click', (e) => {
let existingUsers = JSON.parse(localStorage.getItem("allUsers"));
if (existingUsers == null) existingUsers = [];
let userLogin = document.getElementById('authLogin').value;
let userEmail = document.getElementById('authEmail').value;
let userPassword = document.getElementById('authPassword').value;
let userConfirmPassword = document.getElementById('authConfirmPassword').value;
if (userPassword == userConfirmPassword) {
let users = {
"login": userLogin,
"email": userEmail,
"password": userPassword,
"contacts": []
};
localStorage.setItem("users", JSON.stringify(users));
// Save allEntries back to local storage
existingUsers.push(users);
localStorage.setItem("allUsers", JSON.stringify(existingUsers));
} else {
alert("Password and confirm password must be the same");
}
}, false);
Now, I am looking to create an array of contacts for the chosen user. A contact structure should include the following information:
let userSurname = document.getElementById('_surname').value;
let userName = document.getElementById('_name').value;
let userCountry = document.getElementById('_country').value;
let userCity = document.getElementById('_city').value;
let userEmail = document.getElementById('_email').value;
let userPhone = document.getElementById('_phone').value;
let userWorkPlace = document.getElementById('_workPlace').value;
let contact = {
"surname": userSurname,
"name": userName,
"country": userCountry,
"city": userCity,
"email": userEmail,
"phone": userPhone,
"workPlace": userWorkPlace
};
localStorage.setItem("contact", JSON.stringify(contact));
Please provide guidance on how to achieve this.