I'm storing four user data inputs in local storage with vanilla JavaScript.
Here's what my local storage currently looks like -
User1632832353314: "[{\"first\":\"Joe\",\"last\":\"Rose\",\"email\":\"example@gmail.com\",\"pass\":\"111111111111111111\"}]"
User1632832371314: "[{\"first\":\"Sarah\",\"last\":\"Jones\",\"email\":\"sj@gmail.com\",\"pass\":\"2222222222222222222222\"}]"
I know I can grab each user with the following -
Object.keys(localStorage).forEach(key => {
console.log(localStorage.getItem(key));
const user = localStorage.getItem(key);
// for (const prop in user) {
// console.log(`${prop}: ${user[prop]}`);
// }
});
But I want go down a level and check if an email exists in local storage. I've attempted this with the commented out code and it'll just print out each character of the strings.
With the way my object is stored, using something like key.email
will just return me null.
I've also tried to console.log(JSON.parse(localStorage.getItem(key.email)));
to make the email more acquirable but null is returned even though console.log(JSON.parse(localStorage.getItem(key)));
will return this -
{first: 'Albert', last: 'Chen', email: 'achen@gmail.com', pass: '3333333333333333333333'}
Which is a step in the right direction, but I just want the email.
So what code do I need to include that enables me to communicate with the email key of each object?
EDIT: This is what actually shows up in local storage -
[{"first":"Albert","last":"Chen","email":"achen@gmail.com","pass":"3333333333333333333333"}]
[{"first":"Sarah","last":"Jones","email":"sj@gmail.com","pass":"2222222222222222222222"}]
[{"first":"Sarah","last":"Jones","email":"sj@gmail.com","pass":"4444444444444444444"}]
[{"first":"Joe","last":"Rose","email":"example@gmail.com","pass":"111111111111111111"}]
When I try parse the strings like this -
JSON.parse(user);
I get the following error -
Uncaught SyntaxError: Unexpected token s in JSON at position 0