3

I'm so new in this and trying to create a commercial site on my own. Anyway,this is my object: users:

[{
  "id": "[email protected]",
  "Name": "John Doe",
  "Email": "[email protected]",
  "Password": "john"
},
{
  "id": "[email protected]",
  "Name": "Jane Doe",
  "Email": "[email protected]",
  "Password": "jane"
}]

This is my code:

function addUser(e){
        id = $('#emailAccount').val();

        var name = $('#nameClient').val();      
        var email = $('#emailAccount').val();
        var psword = $('#passwordAccount').val();

        if (name == ""){
            alert("Field name cannot be empty.");   
            e.preventDefault();
        }else if (email == ""){
            alert("Field email cannot be empty.");
            e.preventDefault();
        }else if (psword == ""){
            alert("Field password cannot be empty.")
            e.preventDefault();
        }else {
            users = JSON.parse(localStorage.getItem("users"));

            //Check users

            if(users == null){
                users = [];         
            }
            var userList = JSON.parse(localStorage.getItem("users"));

            //new User object

            var new_user = {
                "id":id,
                "Name": name,               
                "Email": email,
                "Parola": psword                
            }

            users.push(new_user);
            localStorage.setItem('users', JSON.stringify(users));

            console.log("User added")
        }

    }

});

I need to access the id from local storage and I don't know how. Please help me.

2
  • you mean to retrieve record on basis id ?? If this is true , you can't do that. you can get id as users[0].id Commented Jan 27, 2017 at 11:20
  • At which part you need to access the user id? Commented Jan 27, 2017 at 11:22

3 Answers 3

1

Your object object: users: is an array, each element of this array is a user object , so there if you wist to extract id at a particular index you can do something like id = users[index].id

you can also loop through your object get all the ids as follows

var id = [];
for(var i=0;i<users.length;i++){
id.push(users[i].id);    
}

I would advice you to use sessionStorage instead of local storage.

window.sessionStorage.setItem('somekey', value);
window.sessionStorage.getItem('somekey');
Sign up to request clarification or add additional context in comments.

Comments

1

You first need to parse the string using JSON.parse() and then loop through to get every value you need. See the working snippet below:

var dataFromLocalStorage = `[{
  "id": "[email protected]",
  "Name": "John Doe",
  "Email": "[email protected]",
  "Password": "john"
},
{
  "id": "[email protected]",
  "Name": "Jane Doe",
  "Email": "[email protected]",
  "Password": "jane"
}]`;

dataFromLocalStorage = JSON.parse(dataFromLocalStorage);
dataFromLocalStorage.forEach(function(currentVal){
  console.log(currentVal.id);
});

Comments

1

Since you are storing/loading back an array, you need to loop all elements and access each element's property individually, for example:

$.each(users, function(i, user)
{ 
   console.log(user.id);
});

If you need specific elements, you can either access them by their index or use the filter function to get them by a property value.

var foundUsers = users.filter(function(user) {
       return user.id === "[email protected]";
});

The filter function returns an array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.