Brent
Brent

Reputation: 2485

LocalStorage Json String Retrieve

Morning,

I have wrote a function to fetch the data from json and return the value back, but its saying SyntaxError: Unexpected token u

enter image description here Second Function

   function fetchData(u, c, k) {
       return JSON.parse(localStorage[u + c][k]);
    }

Logging

console.log(fetchData(username, centre, "date"));

Error Uncaught

Upvotes: 0

Views: 81

Answers (2)

Barmar
Barmar

Reputation: 780688

It should be:

function fetchData(u, c, k) {
    return JSON.parse(localStorage.getItem(u + c))[k];
}

Upvotes: 3

Anoop
Anoop

Reputation: 23208

Try this

 function fetchData(u, c, k) {
       return JSON.parse(localStorage[u + c])[k];
    }

Upvotes: 2

Related Questions