[英]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": "john_doe@yahoo.com",
"Name": "John Doe",
"Email": "john_doe@yahoo.com",
"Password": "john"
},
{
"id": "janedoe@yahoo.com",
"Name": "Jane Doe",
"Email": "janedoe@yahoo.com",
"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. 我需要从本地存储访问id,我不知道如何。 Please help me.
请帮我。
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 === "john_doe@yahoo.com";
});
The filter function returns an array. filter函数返回一个数组。
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
你的对象
object: users:
是一个数组,这个数组的每个元素都是一个user object
,所以如果你想在特定索引中提取id
,你可以做一些像id = users[index].id
这样的事情。
you can also loop through your object get all the ids as follows 您还可以循环访问您的对象获取所有ID,如下所示
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. 我建议你使用sessionStorage而不是本地存储。
window.sessionStorage.setItem('somekey', value);
window.sessionStorage.getItem('somekey');
You first need to parse the string using JSON.parse()
and then loop through to get every value you need. 首先需要使用
JSON.parse()
解析字符串,然后循环以获取所需的每个值。 See the working snippet below: 请参阅下面的工作代码段:
var dataFromLocalStorage = `[{ "id": "john_doe@yahoo.com", "Name": "John Doe", "Email": "john_doe@yahoo.com", "Password": "john" }, { "id": "janedoe@yahoo.com", "Name": "Jane Doe", "Email": "janedoe@yahoo.com", "Password": "jane" }]`; dataFromLocalStorage = JSON.parse(dataFromLocalStorage); dataFromLocalStorage.forEach(function(currentVal){ console.log(currentVal.id); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.