Iulia
Iulia

Reputation: 31

How can I access an object from an array list of objects?

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.

Upvotes: 3

Views: 79

Answers (3)

james_bond
james_bond

Reputation: 6906

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.

Upvotes: 1

Ionut Necula
Ionut Necula

Reputation: 11480

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);
});

Upvotes: 1

Mitesh Pant
Mitesh Pant

Reputation: 542

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');

Upvotes: 1

Related Questions