0

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

Brent
  • 2,385
  • 10
  • 39
  • 63

2 Answers2

3

It should be:

function fetchData(u, c, k) {
    return JSON.parse(localStorage.getItem(u + c))[k];
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This might be useful, difference between getItem and direct property access: http://stackoverflow.com/questions/12632463/is-localstorage-getitemitem-better-than-localstorage-item-or-localstoragei – Rik Apr 03 '14 at 09:43
2

Try this

 function fetchData(u, c, k) {
       return JSON.parse(localStorage[u + c])[k];
    }
Anoop
  • 23,044
  • 10
  • 62
  • 76