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,235
  • 9
  • 36
  • 61

2 Answers2

3

It should be:

function fetchData(u, c, k) {
    return JSON.parse(localStorage.getItem(u + c))[k];
}
Barmar
  • 669,327
  • 51
  • 454
  • 560
  • 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
  • 22,496
  • 10
  • 60
  • 70