0

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
  • 1
    `console.log(JSON.parse(localStorage.getItem(key)).email)` – Heretic Monkey Sep 28 '21 at 13:02
  • `JSON.parse(localStorage.getItem(key)).email`? – Andy Sep 28 '21 at 13:02
  • `key` is the name of the property in `localStorage`. Why do you expect `key.email` to be the value of the `email` property of the object that is stored under the name (in) `key`? – Andreas Sep 28 '21 at 13:02
  • @HereticMonkey Thank you but this returns undefined. –  Sep 28 '21 at 13:08
  • @Andy Thank you but this also returns undefined. –  Sep 28 '21 at 13:09
  • @Andreas Lack of understanding and other options I guess? –  Sep 28 '21 at 13:10
  • 1
    It shouldn't do. If parse is returning an object like you suggest then you can just get the value from the object: `obj.value`. That said, in your first example, it looks like your localStorage data is an object in an array so try `data[0].email`. – Andy Sep 28 '21 at 13:12
  • The you should definitely have another look at how objects/`localStorage` work – Andreas Sep 28 '21 at 13:13
  • @Andy Sorry. For some reason I can't parse the data anymore. That's why it's returning undefined. Please see my edit above. –  Sep 29 '21 at 13:10
  • @Heretic Monkey Sorry. For some reason I can't parse the data anymore. That's why it's returning undefined. Please see my edit above –  Sep 29 '21 at 13:10
  • Please ask a new question. Any answers to the new state of the question would be different from the answer you've already received. In those circumstances, we require a new question to protect the time of the answerers. Also note that the error you are receiving does not match the data you say is there, so there is something going wrong with the way you are debugging. The error says the first character is `s`, but the first character you are saying is there is `[`. – Heretic Monkey Sep 29 '21 at 14:16

0 Answers0